-2

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?

  • 1
    You have circular inclusion; use forward declarations in your headers instead of `#include`. – ildjarn Mar 14 '12 at 22:41
  • 1
    Circular dependency, forward declarations; that should get you started. – Nim Mar 14 '12 at 22:42
  • possible duplicate of [Circular Dependency in C++](http://stackoverflow.com/questions/4018816/circular-dependency-in-c) – Carl Norum Mar 14 '12 at 22:43
  • The circular dependency post would probably have solved my problem, the issue was I did not know the proper term for this so I couldn't search for it. Thanks! This problem has been bugging me in a larger program for several days now. – Fireborn Silvaranth Mar 14 '12 at 23:30

1 Answers1

1

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:

class A;

class Bclass { 
    // ...
};

and go from there.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111