Hello and welcome to our community! Is this your first visit?
Register
Results 1 to 4 of 4
  1. #1
    Fanatic Enthusiast The Inzuki's Avatar

    Join Date
    Oct 2006
    Location
    ???
    Posts
    4,690

    Header Files in Visual C++

    I recently bought a book called "C++ All-In-One Desk Reference For Dummies" and I'm at the part of learning header files. But here it says:
    "Although I've said very little so far about Microsoft Visual C++ (VC++), if you use VC++, do not add header files to your project. Only add source files. VC++ will keep a listing of the header files in a tree called dependencies, but you don't add them yourself."

    So if I can't add them myself, how will I continue this lesson? Should I use a different software? I tried making a header file in the Header Files folder in my project but the code isn't working. The code is:
    Code:
    string SafeCracker(int SafeID);
    But it says that there is an undefined ';' after SafeCracker. I also tried changing it to void but that messed it up too. The code in main.cpp:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <math.h>
    #include "safestuff.h"
    
    using namespace std;
    
    int main(int argc, char *argv[]){
    	cout << "Surprise, surprise!" << endl;
    	cout << "The combination is (once again)" << endl;
    	cout << SafeCracker(12) << endl;
    	
    	system("PAUSE");
    	return 0;
    }
    
    string SafeCracker(int SafeID){
    	return "13-26-16";
    }
    I'm very confused right now because I probably did something wrong, but the book doesn't go into big detail on anything really.
    hi.

  2. #2
    Fanatic Enthusiast The Inzuki's Avatar

    Join Date
    Oct 2006
    Location
    ???
    Posts
    4,690

    Re: Header Files in Visual C++

    I just thought headers would help a lot when coding, but I guess I don't need it that bad.

    Keep this open, incase some does want to help me out.
    hi.

  3. #3
    Obsessed Veteran Adam's Avatar



    Join Date
    Nov 2004
    Location
    UK / England
    Posts
    17,271

    Re: Header Files in Visual C++

    Header files can be useful, but it takes awhile to get used to them. I made a large project awhile back and damn everytime I changed a parameter set inside the source files the compiler would complain because I forgot to update the header file instance of the function name.

    On the upside header files can be useful for managing includes.

    Your example for your header file.

    Header files must be wrapped in class { } tags along with a name of course.

    i.e

    Code:
    class myExample {
    
    }
    Your example;

    Code:
    string SafeCracker(int SafeID);

    Firstly if this is in a header file you'll need to include all the files / standard namespace inside the header file to support type string. Not the source file. As your function returns a string type. You should also set the scope of the function. Whether it be public, private, protected etc.


    Ie. myExample.h

    Code:
    #include <string>
    using namespace std;
    
    class myExample {
     public:
     string SafeCracker(int SafeID);
    }
    Your source file;
    myExample.cpp


    Code:
    #include "myExample.h"
    
    string SafeCracker(int SafeID)
    {
    
        return; //some string
    }

    Slowly you can see with the use of header files and any additional namespace / includes added to header file become included into the source file, it's good for management, however can be annoying if you update a parameter in a function and forget about updating the header.


    To conclude a header just contains a bunch of function defs, and there scopes, along with various includes. They are not always needed but on med - big projects can become useful.

    Skype: Sounds like Adam wrote that book.

  4. #4
    Fanatic Enthusiast The Inzuki's Avatar

    Join Date
    Oct 2006
    Location
    ???
    Posts
    4,690

    Re: Header Files in Visual C++

    Ah thanks Adam. The book hasn't taught me about classes yet. What a shame.

    And like I said, it didn't go into great detail about header files. It explained some of it and gave me a little bit of code, nothing special.

    Cheers mate
    hi.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •