Thread: Making a header file(something I dont get)

  1. #1
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60

    Making a header file(something I dont get)

    I want to make some header files so I can organize my program a bit better. But im a little confused by it. Ive read the FAQ, some topics here on the forum,s and googled it. Everything I read says to make a new project with the extension '.h'. This is where i'm sonfused. It says to add like prototypes and stuff like that in here, and then the actual code to it in a .cpp file. Im confused because, how do you add prototypes to a file without giving it a .cpp file?

    Are you supposed to make the '.h' file, add a .cpp and give it prototypes. Then make an empty project, give it a .cpp file, include the '.h' file and then just put the code that goes in your prototypes into the newly made .cpp file?

    The stuff I read kept saying to put certain things in the '.h' file and other things in the .cpp file. But since nothing ever said that you have to make another project or anything I had no clue what to do heh.

    (Im using DevC++ btw)
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For Dev-C++, choose File->New source file and call it something.h. Add it to the same project.

    As for what to put into a header file: basiclly you put prototypes in the header file, so that multiple .cpp files can include the header file and get the prototypes for the functions. You usually put the actual functions in a .cpp file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    A graphic example

    Code:
     // header file
    #ifndef HEADER_H
    #define HEADER_H
    
    class Cat
    {
    public:
        Cat();
        ~Cat();
    
        int getAge() const;
        void setAge ( int age );
    
    private:
        int *m_age;
    };
    
    #endif
    Class implement file, note it will be the same name but uses .cpp not .h

    Code:
    #include "header.h"  // needed to acsess class
    
    Cat::Cat()
    {
        m_age = new int(2);
    }
    
    Cat::~Cat()
    {
        delete m_age;
        m_age = NULL;
    }
    
    int Cat::getAge() const
    {
        return *m_age;
    }
    
    void Cat::setAge ( int age )
    {
        *m_age = age;
    
        return age;
    }
    main file follows

    Code:
    #include "header.h"
    #include <iostream>
    
    int main ( void )
    {
        Cat *kitten = new Cat;
    
        std::cout << "My cat is " << kitten->getAge() << std::endl;
    
        kitten->setAge(3);
    
        std::cout << "Now my cat is " << kitten->getAge() << " years old" << std::endl;
    
        delete kitten;
        kitten = NULL;
    
        return 0;
    }
    Yoi can see that using header files you can split a large program up into more
    managle peices

  4. #4
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    I tried doing this:

    Code:
    // header file
    #ifndef HEADER_H // Im using HEADER just as an example, i gave it my own name
    #define HEADER_H
    
    void shop();
    
    #endif
    Code:
    // .cpp for the shop()
    
    #include "header.h"
    #include <iostream>
    
    // code for shop()
    Code:
    // Main .cpp
    
    #include "header.h"
    #include <iostream>
    
    // Along with a few other includes and rest of program.
    But that didnt work. Im guessing I did something wrong, but it looks basically the same to me.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That is the same. You must have done something different without realizing it.

    What kind of errors are you getting? Or what?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by FingerPrint
    I tried doing this:

    Code:
    // header file
    #ifndef HEADER_H // Im using HEADER just as an example, i gave it my own name
    #define HEADER_H
    
    void shop();
    
    #endif
    Code:
    // .cpp for the shop()
    
    #include "header.h"
    #include <iostream>
    
    // code for shop()
    Code:
    // Main .cpp
    
    #include "header.h"
    #include <iostream>
    
    // Along with a few other includes and rest of program.
    But that didnt work. Im guessing I did something wrong, but it looks basically the same to me.
    WHAT didn't work? it failed to compile? the program crashed? it ran but sent porn to your girlfriend? DETAILS!!!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  7. #7
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Well, I may have seen what I did wrong. But ill ask you guys real quick.

    I make the header file IN my main program and just name it 'header.h' right?

    When I try to compile and run it, it gets about a 1/3 of the way through the compilation then says:

    Code:
    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\Pamela Rowley\My Documents\My Pictures\b's folder\LoC\Makefile.win"
    Executing  make...
    make.exe -f "C:\Documents and Settings\Pamela Rowley\My Documents\My Pictures\b's folder\LoC\Makefile.win" all
    g++.exe -D__DEBUG__ -c ../FirstGame/LoC2.0.cpp -o ../LoC/LoC2.0.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"  -I"C:/Dev-Cpp/include/c++/3.4.2/backward"  -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"  -I"C:/Dev-Cpp/include/c++/3.4.2"  -I"C:/Dev-Cpp/include"    -g3
    
    ../FirstGame/LoC2.0.cpp:11:25: myfunctions.h: No such file or directory
    ../FirstGame/LoC2.0.cpp: In function `int main()':
    ../FirstGame/LoC2.0.cpp:1631: error: `shop' undeclared (first use this function)
    
    ../FirstGame/LoC2.0.cpp:1631: error: (Each undeclared identifier is reported only once for each function it appears in.)
    
    make.exe: *** [../LoC/LoC2.0.o] Error 1
    
    Execution terminated
    Also, it says shop() not declared. The original way I had it, it is declared, but with it this way it doesnt work.
    Last edited by FingerPrint; 08-29-2006 at 06:01 PM.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    ../FirstGame/LoC2.0.cpp:11:25: myfunctions.h: No such file or directory
    Are you sure you put
    Code:
    #include "myfunctions.h"
    and not this?
    Code:
    #include <myfunctions.h>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Yes, im positive.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well then myfunction.h doesn't exist, at least not in the same directory as the file you're including it from. Make sure it's in the right directory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Here is what I have.

    Code:
    // Main file for entire program(beginning of it)
    
    #include "myfunctions.h"
    
    #include <iostream>
    #include <stdlib.h>
    #include <cstring>
    #include <stdio.h>
    #include <windows.h>
    #include <fstream>
    #include <time.h>
    #include <string>
    Code:
    // the header file
    
    #ifndef MYFUNCTIONS_H
    #define MYFUNCTIONS_H  
    // Im not sure on this. Since I have void shop(), should my
    // #ifndef and #define both say SHOP_H instead?
    
    void shop(); 
    
    #endif
    Code:
    // the .cpp file for shop()
    // this is only the beginning of it
    
    #include "myfunctions.h"
    #include <iostream>
    Is that how it should be? And I am supposed to have the header file created like a .cpp file but just named header.h and it is supposed to be made in my main program file, correct? I dont make it as a seperate project or anything do I?

    EDIT: I saved them all to the same folder(which is basically the same as the directory, no?)
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is that how it should be? And I am supposed to have the header file created like a .cpp file but just named header.h and it is supposed to be made in my main program file, correct? I dont make it as a seperate project or anything do I?
    That's right, it should be a file with a .h extension in the same directory as your .cpp file(s).

    The only thing I can think of is that maybe your editor added an extension like .cpp to your file (to make it "myfunctions.h.cpp").

    Code:
    // the header file
    
    #ifndef MYFUNCTIONS_H
    #define MYFUNCTIONS_H  
    // Im not sure on this. Since I have void shop(), should my
    // #ifndef and #define both say SHOP_H instead?
    
    void shop(); 
    
    #endif
    You can call the inclusion guards whatever you like, although it's usually related to the name of the file.

    BTW, in C++, <stdlib.h> is <cstdlib>, and the same for all the standard .h files (excluding ones like windows.h).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    Hmmm...I dont know what else to do. I dont get it, if the code is correct why isnt it working? Well, thanks for your help. Ill continue to work on it. If anyone happens to get a bright idea on whats wrong and how to fix it, please post! Thanks.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This error
    Code:
    ../FirstGame/LoC2.0.cpp:11:25: myfunctions.h: No such file or directory
    means that the compiler can't open the file. It (probably) doesn't exist. Whether it has the wrong extension or its sitting in the wrong directory or something else that I can't fathom, I don't know.

    Open explorer and see if the .h file is in the right place with the right name.

    If all else fails try putting the full path in your #include. (Don't keep it this way in case you move it or something, but it might be a good way to tell if it's working.) Something like this:
    Code:
    #include "c:/projects/myfunctions.h"
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Codigious FingerPrint's Avatar
    Join Date
    Mar 2006
    Posts
    60
    AHA! WOOT! I figured it out. I hadnt noticed it before, but they were actually saved in different directories. At some point I made the same file in 2 different places, and saved myfunctions.h to one and shop.cpp along with my main .cpp to the other one.

    Its all good now heh.
    Code:
    /* ------------------------------------------------------------------*/
                               // INSERT CODE HERE
    /* ------------------------------------------------------------------*/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Precompiled header question
    By donglee in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2009, 01:23 AM
  2. IP header "total length" vs. packet size
    By MK27 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-04-2009, 07:45 AM
  3. class header confusion
    By te5la in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 02:39 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Replies: 6
    Last Post: 04-02-2002, 05:46 AM