Results 1 to 32 of 32

Thread: Forward declaration.

  1. #1

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Resolved Forward declaration.

    Do anyone know this topic in and out?

    I have a school assignment, and it is getting pretty big. And I keep getting errors like Object all ready defind in ***.obj. I guess this is because that more then one .h file is including the same class. And even if I use Include guards I am getting this error. I have heard that forward declarations can help me get rid of these problems. Anyone know how to use them properly. Like now the poblem is that I have a main file, that includes a terrain.h file, and the terrain.cpp file aslo includes the terrain.h file, and then the terrain.h file includes the file.h.


    Then I am getting the error:

    error LNK2005: "public: __thiscall cFile::cFile(void)" (??0cFile@@QAE@XZ) already defined in main.obj
    and so on for the rest of the functions in the cFile class (in the file.h)
    Last edited by NoteMe; Nov 11th, 2004 at 12:34 PM.

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    The .h file doesn't include the Definition of the class does it?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Electroman
    The .h file doesn't include the Definition of the class does it?
    What .h file, and what definition?

    The file.h includes the file I/O class, and it's functions, then the terrain.h is only the terrain class definition, and the terrain.cpp is the functions, and then the main.cpp is just Init and Game loop functions and so on.

  4. #4

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Well at least it works if I add everything to the terrain.h file, and don't use a terrain.cpp file. Then it works. But I can't understand why I can't include terrain.h from both main and terrain.ccp. Can't say that it looks like it is circular...

  5. #5
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Not sure if this is the problem, but if you declare the functions in the header, they get declared twice (each time you do #include header.h). This could cause the error you're getting.

    You should define the class (leave the function declarations as prototypes, then have another cpp file, where you implement the class.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  6. #6

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    So all includes should actualy be in the CPP file rather then the H file?`Ohhhh why have I always done it in the H file. But what if I am using stuff in the H file that needs to be included from an other file, will it still work if I just include it from the CPP file?

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by NoteMe
    So all includes should actualy be in the CPP file rather then the H file?`Ohhhh why have I always done it in the H file. But what if I am using stuff in the H file that needs to be included from an other file, will it still work if I just include it from the CPP file?
    Yes, make sure you get the order right though.

    Code:
    #include MyAmazingClassHeader.h
    #include MyAmazingClassDependentHeader.h
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    I myself had that problem awhile back, and I believe still would if I was #include <classname.hpp> twice anywhere in the application.

    I put all my #includes in the header file
    then in the source I just #include <classname.hpp>

    So you are saying I should put ALL includes in the cpp file???
    #include <string>
    #include <vector>

    #include <classname.hpp>

    Like that???

    that will fix that problem???

    (grumble of anger)
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You may and should have #include statements in header files, for declarations you depend on. To rely on the .cpp files to include them in the right order is nothing short of madness.

    Code:
    void foo(); // Function prototype, declaration, to header.
    extern int g_whee; // Variable declaration, to header.
    
    class bang; // Class declaration, to header.
    
    class bang // Class definition, still to header.
    {
      int ding_; // Member declaration.
    
      static int pfft_; // Static member declaration.
    
    public:
      bang(); // Member declaration.
      bang(int i) : ding_(i) {} // Inline member definition, static linkage, header is fine.
    };
    
    void foo() // Function definition, to module.
    {
    }
    
    int g_whee; // Variable definition, to module.
    
    bang noise; // Variable definition, to module.
    
    int bang::pfft; // Variable definition, to module.
    
    bang::bang() { // Function definition, to module.
    }
    Template stuff always goes into headers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by CornedBee
    You may and should have #include statements in header files, for declarations you depend on. To rely on the .cpp files to include them in the right order is nothing short of madness.


    Template stuff always goes into headers.


    So what do you suggest? It is still not working as long as I have a cpp and a h file for the terrain, and both terrain.cpp and main.cpp is including them.

  11. #11
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    I'm at that point as well, where I may need to include my soundengine header in two places....

    I spent lots of time on this problem last time round. I got no where and ended up putting the class into one big header file.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  12. #12
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Okay, it seems as if I an unable to recreate that problem in my new game engine.

    I have #include <clsSfxEngine.hpp> in:
    App_Main.cpp, clsGameEngine.hpp, clsFileParser.hpp, clsLandscape.hpp, clsSfxEngine.cpp

    Not a single error.
    This is not what I previously remember occuring.

    I can post some headers if you want
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    So what do you suggest?
    I suggest your remove all function and variable definitions from your headers. I suggest you make extra-sure that you're not #including any .cpp files anywhere.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    I am sorry....but I get a bit frustrated here. WHen you say definitions do you mean the prototypes or do you mean the code in the functions?


    Am I only supposed to have headers if I have a class definition, and not use headers if I only have a set of functions that I want to "encapsulate" in one file, so I don't loose them?

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The function prototypes go into headers, the function bodies into modules (.cpp).

    If it then still doesn't work, zip the project and upload it, I'll fix it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by CornedBee
    To rely on the .cpp files to include them in the right order is nothing short of madness.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  17. #17

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by CornedBee
    The function prototypes go into headers, the function bodies into modules (.cpp).

    If it then still doesn't work, zip the project and upload it, I'll fix it.

    Thanks....It doesn't work....the project is pretty big. Let me have some minutes on my hand, And I will reacreat the problem in a smaller project. Then it will be easier for you to find the problem, and I can change the real project.

    Thanks a lot....I am getting a friend of mine to mail it to me from school as we speak.

  18. #18

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Just one more Q before I upload this (One of the problems dissapeared when I striped it down.


    If I want to declear a camera object in (lets say) main. Am I then supposed to declear it in main.h, or main.cpp?


    PS: Some of the function prototypes in main.h might or might not use that camera object as a parameter. Not sure if that will make a diffrence.

    ØØ

  19. #19
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I had the same problem a while back and I think this fixed it.
    Code:
    // in terrain.h
    #ifndef TERRAIN_H
    #define TERRAIN_H
    
    // other #includes here
    
    class terrain
    {
        // class prototypes and members
    };
    
    #endif // TERRAIN_H
    and in terrain.cpp and main.cpp just #include "terrain.h"

    That should fix the 'already defined in main.obj' error.

    {EDIT}
    Actually I think what is happening is what has already been pointed out - you are trying to define a method in main.cpp which is already defined in terrain.cpp.

    Anyway, I think you get a compile error instead of a build error without #include guards.
    Last edited by wey97; Nov 11th, 2004 at 10:13 AM.

  20. #20

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    As I said. I am using include guards. I even tried to use it on the actual include in the cpp files.


    Something like this (Saw one of the Halo 2 programmers use it):
    in cpp file:

    [code]

    Code:
    #ifndef _TERRAIN_H
    #include "terrain.h"
    #endif

  21. #21
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You can get the same behavior with this:
    Code:
    // crap.h
    
    #include <iostream>
    using namespace std;
    
    class crap
    {
    public:
    	crap();
    	~crap();
    	void Print();
    };
    Code:
    // crap.cpp
    
    #include "crap.h"
    
    crap::crap(){}
    crap::~crap(){}
    
    void crap::Print()
    {
    	cout << "This is crap" << endl;
    }
    Code:
    // main.cpp
    
    #include "crap.h"
    
    crap c;
    
    void crap::Print()
    {
    	cout << "this is some more crap" << endl;
    }
    
    int main()
    {
    	c.Print();
    
    	system("pause");
    }
    The build error is:
    error LNK2005: "public: void __thiscall crap::Print(void)" (?Print@crap@@QAEXXZ) already defined in main.obj

  22. #22

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Yeah I know...thats why I am using include guards. But when they failed. ThenI didn't see what was wrong and started to ask about forward declarations....because I have heard they can solve problems like this:

    http://www.codeguru.com/forum/showthread.php?t=312455

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    A variable declaration goes into the header, if you want to use it in more than one file. However, be aware that this:
    camera my_camera;
    is a definition.
    This is a declaration:
    extern camera my_camera;
    and, as all declarations, it requires exactly one corresponding definition in a .cpp file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    You know I can't seem to recreate that problem at all.

    THe only difference I can find is that all my header files are in a directory called HEADER and all my source files are in a directory called SOURCE

    Then in the Directories options under the Include section I added the path to my HEADER dir.

    Now any include I want I can just
    #include <clsCrap.hpp>

    I played around trying to get your error, and I am including several includes in many many places with no errors.

    The only other differnce is all my headers are .hpp, not .h


    EDIT: I went as far to add this to at least 4 different header files
    #include <clsForm.hpp>
    #include <clsStatic.hpp>
    #include <clsCmdButton.hpp>
    #include <clsListBox.hpp>

    Those are only suppose to be in my Main.cpp
    I have NO main.h


    And only once did I get an error and i "redefinition class" error.
    Not the standard object already defined error...but an error which read " redifinition of 'class' "...weird.
    Last edited by Halsafar; Nov 11th, 2004 at 11:26 AM.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  25. #25

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    OK...then that must have been the poblem. I liked it better when I had all my varable/struct/class definitions in the .h file. But it works if I am defining them in the cpp file. You can have a look here if you want to see what I did.


    Moving the two lines:

    float *zHeight;
    BMPHeader filHeader;


    from terrain.h to terrain.cpp solved the problem. So I am always supposed to have them in the cpp file then. Even if they are used as parameters in the function prototypes in the h file.


    Here the stripped project is:
    Attached Files Attached Files

  26. #26

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    ANd here is the map over the include files that I have used, no standard lib includes or glut or anything.



  27. #27
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Variables defined in the cpp file?
    say what?
    that can't be....Out of all the books I've read, not once have they ever put the member varables into the cpp file?

    Unless these are not member variables you are refering to.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  28. #28
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Halsafar, he's talking about globals, not members.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  29. #29

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Originally posted by Halsafar
    Variables defined in the cpp file?
    say what?
    that can't be....Out of all the books I've read, not once have they ever put the member varables into the cpp file?

    Unless these are not member variables you are refering to.
    Terrain is not a class. I only have one terrain, so I didn't botter wrapping it up for this "small" assignment. So CornedBee is right. It is not a class.


    ØØ

  30. #30
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    ohhhhhhhhhh

    Then yah, have to be in the cpp file.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  31. #31
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    NoteMe, since it apparently works now, should I still download your project? If I do, I'll probably can't help myself but pick on every little thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  32. #32

    Thread Starter
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Hehehe...well right now, when I have used the delete button on more or less everything I guess it looks kind of funny, and there would be million of things to pick on.

    If we agree that global variable definitions should always be in the cpp file rather then the h file. THen I have at least solved this problem. Even if I hate it. I want them on the same place as my function prototypes. Well can't have it all.


    So you don't need ot download it. Unless you realy want to. It is nothing to run anymore after I deleted most of it. So it is just plain silly code.



    ØØ

Posting Permissions

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



Click Here to Expand Forum to Full Width