0

I have the following situation:

3 C++ Classes:

ClassA ClassB ClassC

class A
{
    int member;
    B bmember;
};


class B
{
    int member;
    std::shared_ptr <A> amember;
    C cmember;
};

class C
{
    int member;
    std::shared_ptr <A> amember;
};

Each class implements functions that use the public member functions of the of other class. The file structure is as follows:

class1.cpp, class1.hpp includes class2.hpp

class2.cpp, class2.hpp includes class3.hpp and class1.hpp

class3.cpp, class3.hpp includes class1.hpp

Using #pragma once in each header file saves me from having an infinite recursive loop. I am building a cmake project that builds each class into a library. They each target eachother.

However because there are no forward declarations of ClassB to ClassA I receive "not declared errors"

I need each class to have a reference to the others, and still be able to access their functions. How can I achieve this? I am willing to merge files if I need too, but I would like to make the libraries modular and not all in one file.

prapin
  • 6,395
  • 5
  • 26
  • 44
Mike Rawding
  • 121
  • 4
  • 2
    Google: "forward declaration". In general do not do that. Dependency cycle means that something is badly designed in code. – Marek R May 28 '21 at 14:41
  • The header for class B and class C does not need to include the header for class A. A forward declaration will be fine. – drescherjm May 28 '21 at 14:56
  • Here is an additional duplicate: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm May 28 '21 at 14:57

0 Answers0