[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;
...
};
}
unindentified classes
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..:
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; };}
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:-Extrarius
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; ... };}
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?
i can just forward a class of a DIFFERENT file like that?
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;}
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;}
[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...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement