Say I have two classes, in two different headers, called:
class TestA
{
public:
int A;
};
class TestB
{
public:
int B;
};
And I want to give them both an assignment operator to each other, so it’s like:
class TestB; //Prototype of B is insufficient to avoid error with A's assignment
class TestA
{
public:
int A;
const TestA &operator=(const TestB& Copy){A = Copy.B; return *this;}
};
class TestB
{
public:
int B;
const TestB &operator=(const TestA& Copy){B = Copy.A; return *this;}
};
How do I do the above whilst avoiding the obvious error that will result from calling/using class TestB when it hasn’t been defined yet?
You cannot have the function definitions in your files as written as that will require a circular dependency.
To solve, forward declare the classes and put their implementation in a separate files.
A‘s header file:A‘s implementation file:Follow the same basic structure for
Bas well.