I've really tried to make this work without using pointers. I want to create a shared class where two classes "see" each other and share this object. I wanted to initialize the shared object using an initializer list but I'm getting these errors.
error: field ‘shared_type’ has incomplete type ‘shared’
note: forward declaration of ‘class shared’
note: forward declaration of ‘class class_A’
note: forward declaration of ‘class class_B’
The first error doesn't make sense because the constructor for shared
exists in shared.cpp.
I have tried declaring shared_type in class_A and class_B
as const shared& shared_type; but that gave me an error saying that the lvalue is a const and the rvalue cannot be set to const, namely the value for the initializer. Is there anything else I could try without using pointers?
This is the complete code.
main.cpp
#include
#include "include/class_A.h"
#include "include/class_B.h"
int main()
{
return 0;
}
class_A.h
#ifndef CLASS_A_H
#define CLASS_A_H
//#include "shared.h"
class shared;
class class_A
{
public:
shared shared_type;
class_A();
~class_A();
protected:
private:
};
#endif // CLASS_A_H
class_A.cpp
#include "class_A.h"
class_A::class_A() : shared_type()//this is the initializer for class_A
{
//ctor
}
class_A::~class_A()
{
//dtor
}
class_B.h
#ifndef CLASS_B_H
#define CLASS_B_H
//#include "shared.h"
class shared;
class class_B
{
public:
const shared& shared_type;
class_B();
~class_B();
protected:
private:
};
#endif // CLASS_B_H
class_B.cpp
#include "class_B.h"
class_B::class_B() : shared_type()//this is the initializer for class_B
{
//ctor
}
class_B::~class_B()
{
//dtor
}
shared.h
#ifndef SHARED_H_
#define SHARED_H_
//#include "class_A.h"
//#include "class_B.h"
class class_A;
class class_B;
class shared
{
private:
class_A type_A;
class_B type_B;
public:
shared();
~shared();
};
#endif
shared.cpp
#include "shared.h"
shared::shared(){}
shared::~shared(){}