I’m trying to implement a class that has pointers for class members and methods that return pointers but on compile I get “syntax error : missing ‘;’ before ‘*'” and “missing type specifier – int assumed. Note: C++ does not support default-int” errors
Here’s the code:
Main.cpp:
#include "AClass.h"
#include "BClass.h"
int main ( int argc, const char* argv[] )
{
AClass a;
BClass b;
return 0;
}
AClass.h:
#ifndef ACLASS_H
#define ACLASS_H
#include "BClass.h"
class AClass
{
public:
BClass* getB ();
void setB (BClass* inst);
private:
BClass* b;
};
#endif
BClass.h:
#ifndef BCLASS_H
#define BCLASS_H
#include "AClass.h"
class BClass
{
public:
AClass* getA ();
void setA (AClass* inst);
private:
AClass* a;
};
#endif
I haven’t even fleshed out the classes with cpp files and I get a string of errors:
It doesn’t matter if I create the C++ files and define everything, these errors remain.
1>------ Build started: Project: memberUDFpointers, Configuration: Debug Win32 ------
1> Main.cpp
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C2143: syntax error : missing ';' before '*'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(9): warning C4183: 'getA': missing return type; assumed to be a member function returning 'int'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(10): error C2061: syntax error : identifier 'AClass'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C2143: syntax error : missing ';' before '*'
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\documents\cpp projects\memberudfpointers\memberudfpointers\bclass.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I’ve looked at many different posts all over but am still scratching my head on this one.
Can someone give me a clue?
Your definition of AClass depends on the definition of BClass, and the definition of BClass depends on the definition of AClass. You can’t define one before the other has already been defined.
Fortunately, the actual classes only use pointers to the other, so you can just declare one class:
and go from there.