Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

unindentified classes

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!