user366312
user366312

Reputation: 16904

How to use circularly dependent classes in C++?

This code is not compiling.

What modification can I do to achieve the desired result?

ClassOne.h

#ifndef _CLASS_ONE_
#define _CLASS_ONE_

#include <string>
#include "ClassTwo.h"

class ClassTwo;

class ClassOne
{
private:
    string message;     
    friend ClassTwo;
    ClassTwo m_ClassTwo;

public:
    ClassOne();
    void Display();
};

#endif

ClassTwo.h

#ifndef _CLASS_TWO_
#define _CLASS_TWO_

#include <string>
#include "ClassOne.h"

class ClassOne;

class ClassTwo
{
private:
    string message;
    friend ClassOne;
    ClassOne m_ClassOne;

public:
    ClassTwo();
    void Display();
};

#endif

ClassOne.cpp

#include "ClassOne.h"
#include "ClassTwo.h"
#include <iostream>

ClassOne :: ClassOne()
{
    std::cout<<"ClassOne()...called\n";
    this->m_ClassTwo.message = "ClassOne - Message\n";
}

void ClassOne :: Display()
{
    std::cout<<this->m_ClassTwo.message;
}

ClassTwo.cpp

#include "ClassTwo.h"
#include "ClassOne.h"
#include <iostream>

ClassTwo :: ClassTwo()
{
    std::cout<<"ClassTwo()...called\n";
    this->m_ClassOne.message = "ClassTwo - Message\n";
}

void ClassTwo :: Display()
{
    std::cout<<this->m_ClassOne.message;
}

main.cpp

#include "ClassOne.h"
#include "ClassTwo.h"

int main()
{
    ClassOne one;
    one.Display();

    ClassTwo two;
    two.Display();
}

Error messages

1   error C2146: syntax error : missing ';' before identifier 'message'
2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
4   error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
5   error C2146: syntax error : missing ';' before identifier 'message'
6   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
7   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
8   error C2039: 'message' : is not a member of 'ClassTwo'
9   error C2039: 'message' : is not a member of 'ClassTwo'
10  error C2146: syntax error : missing ';' before identifier 'message'
11  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
12  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
13  error C2079: 'ClassOne::m_ClassTwo' uses undefined class 'ClassTwo'
14  error C2146: syntax error : missing ';' before identifier 'message'
15  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
16  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
17  error C2039: 'message' : is not a member of 'ClassOne'
18  error C2039: 'message' : is not a member of 'ClassOne'
19  error C2146: syntax error : missing ';' before identifier 'message'
20  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
21  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
22  error C2079: 'ClassTwo::m_ClassOne' uses undefined class 'ClassOne'
23  error C2146: syntax error : missing ';' before identifier 'message'
24  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
25  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Upvotes: 2

Views: 1997

Answers (3)

martiert
martiert

Reputation: 741

As said, you need to forward declare at least one of ClassOne or ClassTwo.

You ClassOne.h could therefor look like:

#ifndef _CLASS_ONE_
#define _CLASS_ONE_

#include <string>

class ClassTwo;

class ClassOne
{
private:
    string message;
    ClassTwo* m_ClassTwo;

public:
    ClassOne();
    void Display();
};

#endif

As you can see, we declare ClassTwo, but do not include it. We basically only tell the compiler that yes, we do have a ClassTwo, but we don't really care what it contains right now.

Also look at your ClassTwo member, this is now a pointer. The reason is that a member would require the compiler to know the size of the object, which we currently have no clue what is. Therefor you need either a pointer or a ref.

Next, in your ClassOne.cpp, would will need to include ClassTwo.h, to get the functions and size of that class.

A few things though: With a forward declaration you can not inherit from ClassTwo, use the methods of the forwarded class in the header file (How would the compiler know which methods exists?) Define functions or methods using the forwarded class, that is you have to pass by reference or pass a pointer.

Upvotes: 3

ivanzoid
ivanzoid

Reputation: 6052

1) Use forward declarations:

add

class ClassTwo;

to ClassOne.h

and

class ClassTwo;

to ClassTwo.h

2) You need to create your member variables (or at least one of them) dynamically, using operator new, or if you wish, with one of the smart-pointers, like boost::shared_ptr from boost library or std::shared_ptr from standard library if you use C++11.

Upvotes: 1

Puppy
Puppy

Reputation: 146910

You cannot, ever, compile that code. You have described a system where type A contains type B, and type B contains type A. Therefore, both type A and B recursively and infinitely contain themselves, which is impossible. You must fundamentally re-architect your code to eliminate this problem.

Also, your friend syntax is wrong.

Upvotes: 6

Related Questions