0

I have got four classes A, B, C and D.

A.h:

#include "B.h"
#include "C.h"
#include "D.h"

class A
{
    public:
        A() {};
        void run() { B b; C c(b); };
};

B.h:

#include "D.h"

class B
{
    public:
        B() {};
};

C.h:

#include "B.h"

class C
{
    public:
        C(B b) { m_copy_b = &b; };
        int blank(B& b) {}; // LINE 10
    private:
        B* m_copy_b; // LINE 12

};

D.h:

#include "C.h"

class D
{
    public:
        D() {};
};

And my main file:

#include "A.h"

int main()
{
    A a;
    a.run();

    return 0;
}

Apart from the fact that the code sucks (it's just for the example), I get two errors while compiling the code:

include\C.h|10|error: 'B' has not been declared
include\C.h|12|error: 'B' does not name a type

I don't see how I could fix it, it worked well before I add the class D...

Any idea? Thanks!

  • You should read the duplicate post and use "forward declarations" – Cory Kramer Jul 07 '16 at 19:55
  • If I replace ``#include "B.h"` by `class B;` in `C.h`, then I get an error saying that b has an incomplete type. Besides, I can't use in `C.h` any methods calling a method of class B because incomplete declaration. –  Jul 07 '16 at 20:13
  • Anyway I don't see where the problem is because there is circular references between B, C and D but the size of an instance of C should be known as there only is a pointer to B, not an instance of B. –  Jul 07 '16 at 20:13
  • Make sure you use guards such as `#pragma once` at the top of each header file. – Archie Gertsman Jul 07 '16 at 20:51
  • I have the headers and it still fails to compile. –  Jul 07 '16 at 20:59

0 Answers0