I have a base interface class:
class A
{
public:
ITask(){}
virtual bool Start()=0;
virtual void Update()=0;
virtual void Stop()=0;
};
I now have 2 other classes, that inherit from this
#include "A.h"
#include "C.h"
class B: public A
{
public:
bool Start(){}
void Update()
{
c.Start();
}
void Stop(){}
static bool m_run;
static void SetRun(bool run)
{
m_run = run;
}
private:
C c;
};
lastly I have a 3rd class:
#include "A.h"
#include "B.h"
class C : public A
{
public:
bool Start()
{
B::SetRun(false); // cant do this
B::m_run = false; // or this
}
void Update()
{
}
void Stop()
{
}
}
I have shaved down some of the code, for simplicity.
I dont understand why I cant access the static var in B. Do I need to make it a pointer or a ref?
I get 2 errors:
error C2653: 'B' : is not a class or namespace name
error C3861: 'm_run': identifier not found
Although you don’t show it, I’m assuming that
B.hincludesC.h; otherwise the lineC c;won’t compile. This causes a circular dependency in the header files:B.hmust be included beforeC.h, which must be included beforeB.h, which is impossible.The easiest solution is to move the body of
C::Startout of the definition ofC, so thatC.hdoes not need to includeB.h. The function definition can go into a source file, or a separate header if you want to keep it inline.Alternatively, you could modify
Bto contain astd::unique_ptr<C>rather than an instance ofC, and implement a constructor (in a source file, or a separate header) that initialises it withnew C. ThenB.honly needs to forward declareclass C;rather than includingC.h.A better solution, if possible, would be to rethink the relationships between the classes so that there isn’t a circular dependency.
(UPDATE: while I was writing this answer, the question changed to show that
B.hdoes indeed includeC.has I guessed.)