0

I am sorry for asking this question, I know there are a lot of similar questions online, but I wasn't able to solve my problem. My files look something like this:

File class_a.h:

#ifndef CLASS_A_H
#define CLASS_A_H

#include "class_b.h"

class class_a{

    //...
    class subclass_of_a{
        
         class_b obj_b;

    }
}

File class_a.cpp:

#include "class_a.h"

//...

class_a::subclass_of_a(...) : obj_b(...){
    //...
}

File class_b.h:

#ifndef CLASS_B_H
#define CLASS_B_H

#include "class_a.h"

class class_b{

    double function(class_a::subclass_of_a const &obj_a);

}

This results in following error:

’class_a’ does not name a type

I hope you can help me with this problem.

mc.math
  • 13
  • 3
  • In `class_a.cpp`, do you have `#include "class_a.h"` at the top? – NathanOliver Jun 16 '21 at 15:49
  • Oh no... you have **circular includes**. `class_a.h` insists that `class_b.h` **must** be included first. And `class_b.h` insists that `class_a.h` **must** be included first. Those can't both be true. – Drew Dormann Jun 16 '21 at 15:50
  • `class_a::subclass_of_a(...) : obj_b(...)` is invalid syntax.. you can't initialize `obj_b` like that from a function. – Brandon Jun 16 '21 at 15:51
  • That's a bog-standard circular inclusion problem. Unfortunately you can't solve it through the normal forward declaration solution since `class_a::subclass_of_a::obj_b` is an *instance* of `class_b` instead of a reference or a pointer. You need to rethink your design and the need for the classes to be interconnected this way. – Some programmer dude Jun 16 '21 at 15:51
  • Tell me if that duplicate works for you. – Guillaume Racicot Jun 16 '21 at 15:52
  • 1
    @DrewDormann Thank you for pointing that out, I misses to copy that. I have now edited the post. – mc.math Jun 16 '21 at 15:52

0 Answers0