Skip to main content
GameDev.net gamedev.net
🔒 Locked

Classes, headers, forums... oh my

Started by ToohrVyk Oct 29, 2002 at 5:05 AM 2 replies 200+ views
Original Post
ToohrVyk
ToohrVyk
I saw a thread on this subjects only weeks ago, and read through it. I thought I understood it. Apparently I didn''t. I have to declare two classes, in two different header files, each class containing an instance of the other one (actually, a pointer). And now I don''t seem to find that thread. Could someone point it out for me, or explain everything again? - Including the other class'' header causes the error: "unexpected ''class''". - Declaring the other class at the beginning of the header file, say: [script] class A; class B { public: A * pA; }; [script] ends up in an error on ''class A;'' Thank you very much. Extatica is coming soon! Check it out on: http://www.extatica.com02.com Nexeruza Studios: http://nexeruza.ionichost.com/home.html
Zipster
Zipster
As long as you are only storing pointers to each class, then the forward declaration - the "class A;" you put at top - is all you need. Make sure you aren''t trying to access any members within an inlined function or something.

What kind of errors are you getting?


  
// Header A

class B;
class A
{
public:
B *pB;
};

// Header B

class A;
class B
{
public:
A *pA;
};
nicho_tedja
nicho_tedja
errors happen in .cpp files. .h never get compiled, unless they are #includ-ed. so, this is what I do:


  
// somefile.cpp

class A;
class B;

#include "class_A.h"
#include "class_B.h"


i think it''s much easier to follow:
class A; // there exist class A and B
class B; // where?

#include "class_A.h" // here
#include "class_B.h" // and here

instead of:
// class_A.h
class B; // there exists class B, where?
class A // somewhere else, this is class A.
{

}

anyway, it actually depends on your coding style.


My compiler generates one error message: "does not compile."
My compiler generates one error message: "does not compile."
ToohrVyk
ToohrVyk
Thank you! I found the old thread, btw.
Now it works.

Extatica is coming soon!
Check it out on:
http://www.extatica.com02.com
Nexeruza Studios:
http://nexeruza.ionichost.com/home.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.