Sign in to follow this  
BloodLust666

unindentified classes

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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this