My Problem:
Got two classes, class A and B, so i got A.h and A.cpp and B.h and B.cpp. A needs to know B and B needs to know A. I solved it the following way (i don't know why it has to be so...)
A.h:
#include "B.h"
class A{ ...
A.cpp:
#include "A.h"
B.h:
#include "A.h"
class A; // forward declaration
class B { ...
B.cpp:
#include "B.h"
I used one forward declaration and it works.
The Problem is, that both classes need to be in the namespace "ui". Or at least I think this is the meaning:
A.h:
#include "B.h"
namespace ui{
class A;
}
class A{ ...
B.h:
#include "A.h"
namespace ui{
class B;
}
class B{ ...
This doesn't work anymore. What do I have to do now to make it work again with namespace and forward declaration?
Both have to be in this namespace. I'm working with Qt and the lines "namespace ui{" etc. are needed. And both classes need to know each other. I already tried just to make this:
namespace ui{
class A;
class B;
}
in both headers, but this doesn't work...
Btw: All Header-Files also got the "ifndef"-mechanism.
If I understand you right, you declare 2 different types with such a construct:
#include "B.h"
namespace ui{
class A;
}
class A{ ... };
Here you state that there is a class A
in the namespace ui
, but you define a class A
in the global namespace. Therefore ui::A
is declared but never defined. Move the definition of A
to the ui
namespace as well:
#include "B.h"
namespace ui{
class A;
}
//somewhere later:
namespace ui
{
class A{ ... };
}