Firstly you should post your code if you are getting errors for us to be able to provide proper help.
However this is a common issue. I expect that you have defined you class methods in the class definition (in the .h file) rather than just declared the class methods in the class definition (in the .h file) and then defined the class methods in a separate .cpp file.
The problem is that you can't access a class until you have seen its definition but if the definition of class A requires the definition of class B and the definition of class B requires the definition of class A then yoiu get stuck because both A and B require B and A to be defined first which is not possible.
Example (wont compile
-
class A
-
{
-
public:
-
int GetData()
-
{
-
return data;
-
}
-
-
void CopyB(B& b)
-
{
-
data = b.GetData();
-
}
-
-
private:
-
int data;
-
};
-
-
class B
-
{
-
public:
-
int GetData()
-
{
-
return data;
-
}
-
-
void CopyA(A& a)
-
{
-
data = a.GetData();
-
}
-
-
private:
-
int data;
-
};
-
Produces: bytes.cpp:9: error: 'B' has not been declared
However normally you only require the definition of a class where you access its members and that is only inside the methods. If you move the definition of the methods out of the classes then you get
-
class A
-
{
-
public:
-
int GetData();
-
void CopyB(B& b);
-
-
private:
-
int data;
-
};
-
-
class B
-
{
-
public:
-
int GetData();
-
void CopyA(A& a);
-
-
private:
-
int data;
-
};
-
Compile that and you get the same error as before but now the definition of class A only uses a reference to class B and you can use a forward declaration to class B like this
-
class B; // <----- Forward declaration
-
-
class A
-
{
-
public:
-
int GetData();
-
void CopyB(B& b);
-
-
private:
-
int data;
-
};
-
-
class B
-
{
-
public:
-
int GetData();
-
void CopyA(A& a);
-
-
private:
-
int data;
-
};
The forward declaration tells the compiler that B is a class name and provides enough information for the compiler to create pointers and references to class B, since for the class definition all you need is a reference that is enough to allow the definition of class A to compile and that allow the following definition of class B to compile.
You then put the definitions of the methods after both the definitions of the classes
-
int A::GetData()
-
{
-
return data;
-
}
-
-
void A::CopyB(B& b)
-
{
-
data = b.GetData();
-
}
-
-
int B::GetData()
-
{
-
return data;
-
}
-
-
void B::CopyA(A& a)
-
{
-
data = a.GetData();
-
}
-
You always get this problem when you have a circular reference of classes and the solution is always to put the definitions of the methods into separate files, make sure the class definitions only use references or pointers to the other classes and use forward declarations to resolve the issues.