Thread: creating header files

  1. #1
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38

    creating header files

    Hi im new to the site and pretty new to C++ programming
    im trying to teach myself and there is afew fuzzy points but any ways

    im trying to make a pretty simple text adventure game and to make the main source code smaller i figured i'd make my own header files to activate throughout the main app

    when i tried it with a simple one line cout function it worked fine but then i tried to initialize variables for things like setting up damage and what not and the compiler kept giving me an error that said something like....

    ERROR: error in header, reason: code in header

    i looked at my compilers help and it said something about maybe how i initialized the variables, but it talked mainly about C to C++ transitioning.

    will someone explain why this happening? Thank You.

    Compiler: Borland C++ Builder
    OS: Windows XP

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb Check the FAQ

    The Programming FAQ has an example.

    Typically, you'll have two files... MyHeader.h and MyHeader.cpp. And, your functions would be defined in the cpp file. I didn't think it was an actual "error" to put "code" (functions?) in the .h file.

    [EDIT]
    Also, before you define a varaible in one place and try to use it in another, make sure you understand variable scope. If you don't know about scope, there is an introduction to scope in this tutorial
    Last edited by DougDbug; 01-04-2005 at 05:24 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Header files contain declarations.
    Code:
    // this is foo.h
    #ifndef FOO_H_INCLUDED
    #define FOO_H_INCLUDED
    void foo ( char *msg );
    #endif
    Source files contain definitions
    Code:
    //This is foo.cpp
    #include <iostream>
    #include "foo.h"
    void foo ( char *msg ) {
      std::cout << msg << std::endl;
    }
    Code:
    // this is main.cpp
    #include <iostream>
    #include "foo.h"
    int main ( ) {
      foo ( "hello world" );
    }
    To compile this, you would type
    bcc32 foo.cpp main.cpp

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>when i tried it with a simple one line cout function it worked fine
    As I interpret this, what you are trying to do is:
    (someheader.h)
    Code:
    std::cout << "This was run from someheader.h" << std::endl;
    (main.cpp)
    Code:
    int main()
    {
       #include "someheader.h"
       return 0;
    }
    I hope that's not what you're doing, because it's a totally incorrect usage of headers. If it is, just say so and I'll help out more - but in case I'm wrong about what you're doing, I don't feel like typing up an essay and then finding out that it was totally irrelevant
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    yeah hunter2 that iswhat im doing only the code you typed was wrong i would include a simple cout function called something like hello_world() and put it in the header file then i included the header into my .cpp file and typed code like this:

    int main()
    {
    hello_world();
    getch();
    return 0;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Using #include to include code written in other modules is a really bad idea.

    Unless of course said code is of either the "inline" or "template" variety.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    Unless of course said code is of either the "inline" or "template" variety.
    what are those?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    functions defined in header files are considered to be inlined (you can also inline functions in cpp files by using the keyword inline). The compiler has the option of actually inlining the functions or not, even if you explicitly indicate it should do so. Inlining functions means that the function body is repeated in the code verbatim each time in the program rather than calling the function. Because the function body is directly embedded in the code it tends to be a bit faster, but can bloat the source code significantly, particularly if the inlined functions are rather long.

    templates are a mechanism to create code that is more "generic". Methods of templated classes must be defined in the header file, for reasons which have been explained to me in the past but I forget at the moment. If you don't know about templates yet, don't worry about them. Just remember that there's an exception to every rule, and definining templated class methods in header files is one of the exceptions to the don't define functions in header rule unless you intend them to be inlined rule.
    You're only born perfect.

  9. #9
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Those are advanced issues that you probably don't want to try to tackle at this point since you are just really starting out. They are very useful things to learn, but you need to have a good grasp of the basics before you jump into templates or inline code.

    As mentioned before by Hunter and Salem, it is a bad idea to put actual code into header files (until you begin doing some more intermediate-advanced programming). Instead, header files should be used when you need to include the declarations for a function (or functions) in multiple source files.

    For example, in your text adventure game, you may want to break your program up into different parts. In one part you will put the functions that will handle moving from room to room, and in another part you will put the functions that handle fighting monsters. Instead of having one huge file with these functions all together, it is helpful to break those parts into separate files, say "movement.cpp" and "battle.cpp". Now say that some of the functions in "battle.cpp" will need to use some of the functions in "movement.cpp" (monster wants to run away so needs to move to another room). Conceptually you want to keep the functions in separate files (makes it easier to design your program this way), but you need a way to let "battle.cpp" know how to call the functions in the other file. This is what headers are for.

    Instead of you typing out
    Code:
    int movetoroom(int newroom);
    bool validexit(int direction);
    at the top of both files, you move these two lines into "movement.h" and
    Code:
    #include "movement.h"
    in the battle.cpp file. Now, if you add more functions to "movement.cpp", you simply update "movement.h", and "battle.cpp" automatically sees the new functions without you having to change a single thing in "battle.cpp".

    As the FAQ DougDbug pointed out, this is also a way for you to more easily re-use your code. When you make the sequal to your text adventure game, you may want to use some of the same functions. For example, if you want to use "movement.cpp" again so you don't have to re-do all the functions, you would simply
    Code:
    #include "movement.h"
    in the new program files that need them, and link "movement.cpp" with your main file. All the work and debugging you put into making "movement.cpp" do what you want is now re-done by typing one line in your new source file.

    And in fact, this is exactly what you are doing when you include the standard headers like <string> and <iostream>. The designers of the standard libraries wrote and debugged those functions like cout and now they are available to us by just #include'ing them. Because they are part of the standard C++ library however, you don't have to do anything special beyond putting the appropriate #include in your program file. The compiler automatically links them. With your own files you have to link them yourself as Salem showed.

  10. #10
    Self-Taught Noob
    Join Date
    Jan 2005
    Location
    Ohio
    Posts
    38
    i have a question how do youput two source codes in your project and start with one and switch to another?
    No one died when Clinton lied.

    Compiler: Borland C++ Builder
    OS: Windows XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM