// header1.h
class b;
class a {
public:
void func(b* item);
}
class c : public a {
void Execute();
}
// header2.h
// Add what here?
class b {
public:
void func(c* item)
{
item->Execute();
}
}
Before you said that the definitions of a and c are in header1.h, but now you instead forward declare them and you show a compiler error about them not being defined? You're really not making sense now. You sure header1.h does NOT include header2.h?
#ifndef HEADER1_H_
#define HEADER1_H_
#include "header2.h"
#include <iostream>
class b;
class a {
public:
void func(b* item);
};
class c : public a {
public:
void Execute() { std::cout << "Hello World"; }
};
#endif
Generally, you can use: class A; to forward declare a pointer only. If you want to use a method or access a member, you need to #include that class's header.
This is how I do it:
1 2 3 4 5 6 7 8 9 10 11
class B;
class A
{
B* b;
public:
void method(); // this will manipulate b, but we don't say that here;
};
class C : public A
{
// ...
};
1 2 3 4 5 6 7
class C; // You don't need to know about class A here
class B;
{
C* c;
pubic:
void method();
}
1 2 3 4 5 6 7
#include "classB.h"
#include "classAC.h" // This is only place we need the full declaration of class A and C
void B::method()
{
c->method();
}
I need to use singleton classes for states. So I can't just declare it inside the class even if that would work. If you are interested to help me further you should take a look at the code.
If you are interested to help me further you should take a look at the code.
If you are interested in being helped, you should pay attention to what is being said to you. You cannot define methods in the class (A) which require the full definition of another class (B) for which the full definition (of class B) is not available. The obvious solution is to move those method definitions outside of the class definition. One usually puts them into a source (.cpp) file. In that source file, one may #include whatever headers are necessary, because those headers will not be including each other. This also has the benefit of separating the interface (in the header file) from the implementation (in the source file.)