<solved> Errors: C2143, C4430 but it is included
-
I got the error above for following code:
@
// A.cpp:
#include "A.h"
#include "B.h"A::A( B &ref_b)
: b( ref_b )
{}void A::create_T()
{ t = new T(); }
//EOF// A.h:
#ifndef A_H
#define A_H#include "T.h"
#include "B.h"class A {
public:
A( B &b);
~A();
void create_T();private:
B &b;
T *t;
};
#endif //EOF// B.cpp:
#include "B.h"
#include "A.h"B *B::m_pInstance = NULL;
B::B( void )
{}B *B::GetInstance()
{
if( !m_pInstance )
{
m_pInstance = new B();
}
return m_pInstance;
}
// EOF//B.h:
#ifndef B_H
#define B_H#include "A.h"
class B : public QThread
{
public:
B( void );
virtual ~B( void );
static B *GetInstance();private: A *a; static B *b;
};
#endif
// EOF// T.cpp:
#include "T.h"
#include <iostream>using namespace std;
T::T()
{
this->ptr_Special = NULL;void T::acceptConnection()
{}void T::startRead()
{}
// EOF//T.h:
#ifndef T_H
#define T_H#include "A.h"
class T: public QObject
{
public:
T();
~T();public slots:
void acceptConnection();
void startRead();private:
A *ptr_Special;
};
#endif //EOF
@When I have ptr_Special locally in a method, instead of in the Header, I can compile that stuff..
Any suggestions?
Cheers Huck
-
Thank you, I included the corresponding headers in one Class and then I forwarded the required class' in the other classes.. But in T.cpp for e.g. @ptr_Special->callMethod();@ then I further get an:
C2027: use of undefined type 'A'
C2227: left of '->callMethod' must point to class/struct/union/generic type -
You forward-declared the A class, which only declares pointers of that type within your class T. Since pointers all have the same size this is ok. Here, you are attempting to use the class A, by calling callMethod(), without actually defining what A is. Something like:@A *ptr_Special = new A();@
When it comes to using this class the compiler has to know more about the A class, at least its size. Unfortunately, no information is accessible other than that there is a class called A. So I would recommend you put #include statements for the already forwarded class A in class T's source T.cpp.
--> forward declaration in the T.h : class A; // to prohibit circling includes
--> accordingly include in its T.cpp: #include "A.h" // to tell the pointers required space--------> Correspondingly for all the other forward declared classes, too.
hth!