unindentified classes

This topic is 4494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

i have 2 classes (both in the same namespace but if different headers). In the first class, i have a pointer to the second class; and in the second class, i have an object of the first class. i tried forward declaration but nothings working... How could i fix this? Here's basically what i have..:
[source lang=c++]
~~~file1.h~~
#include "file2.h"

namespace NS
{
class first
{
second* m_pvar;
...
};
}

~~~ file2.h~~~
#include "file1.h"

namespace NS
{
class second
{
first m_var;
...
};
}



Share on other sites
It doesn't look like you have a forward declaration to me. In file1.h you need something like:
namespace NS {  class second;  class first {    second* m_pvar;  };}

Share on other sites
Because file2.h actually uses an instance of the object, the object must be actually declared before that point.
However, file1.h only uses a pointer to the instance of the object, so it only needs to know what kind of object it is, so file1.h should look like:
namespace NS{    class second;    class first    {        second* m_pvar;        ...    };}
-Extrarius

Share on other sites
i took out the forwards to show the basic idea of what i had.

i can just forward a class of a DIFFERENT file like that?

Share on other sites
Yes, that's the point of havig a forward declaration.

Share on other sites
bahh.... now when i do that, it says the extern variable i have in file1 (which is an object in file2) is unidentified...

[source lang=c++]~~~file1.h~~#include "file2.h"namespace NS{    class Second;    class First    {        Second* m_pVar;        ...        Function() {Global.dosomething();};    };}~~~ file2.h~~~#include "file1.h"namespace NS{    class Second    {        First m_Var;        ...    };    class Third    {       ...    };    extern third Global;}

Share on other sites
Now you might want to separate out the Third class into it's own header.

Share on other sites
woops... my mistake... it's the global is actually of class "second" (this is kinda confusing with the actual names and code i have :-p)

[source lang=c++]~~~file1.h~~#include "file2.h"namespace NS{    class Second;    class First    {        Second* m_pVar;        ...        Function() {Global.dosomething();};    };}~~~ file2.h~~~#include "file1.h"namespace NS{    class Second    {        First m_Var;        ...    };    extern Second Global;}

Share on other sites
wait... i'm just gonna use the actual names in the next post. here:

Share on other sites
[source lang=c++]~~~Graphics.h~~#include "Particles.h"namespace Engine{    class cTexture    {        ...    };    class cGraphics    {        cParticleSytem PS;    };extern cGraphics Graphics;}~~~ Particles.h~~~#include "Graphics.h"namespace Engine{    class cTexture; // forward    class cEmitter    {        cTexture* m_pTex;        ...        Function() {Graphics.DoSomething();};    };    class cParticleSystem    {       ...    };}

THAT'S how it is...

Share on other sites
Use the forward declaration and put the definition of cEmitter::Function() in a source file instead of the header.

Share on other sites
forward declare a function? what's the syntax of that?

note: of course the DoSomething function is in the class cGraphics

class cGraphics
{
cParticleSytem PS;

DoSomething();
};

forgot that too...

is that what you meant by forward declaring that function?

Share on other sites
OHH! i see what you mean. don't have it inline like that, just define it, then put the code in the .cpp file. i'll try that

Share on other sites
sweet, that fixed the Global variable thing, but i'm still having a problem with the ParticleSystem still being an undeclared identifier...

Share on other sites
Seems like a good time to put cParticleSystem into its own header.