I'm making a c++ OpenGL project and I'm having trouble with static variables.
I have a header "Scene.h" like so:
#pragma once
#include "A.h"
class Scene
{
//class body
};
static Scene* active = new Scene();
And my A.h file looks like this:
#pragma once
#include "Scene.h"
class A
{
active->SomeMethod(); //Here I get error C2065: undeclared identifier
};
In my source file I have included only my Scene.h, since it includes A.h already, and I have no problem there.
I have also tried to use a static Scene object like so:
class Scene
{
static Scene* active;
};
And then to access it like so:
Scene::active->DoSomething();
But then I get error C2653: Scene is not a class or namespace name. I read somewhere that to do this I need precompiled headers, and that is no option for me.
What is the correct way to have a static pointer in this case?
active->SomeMethod();(whatever that is meant to be)staticvariables in header, make theminline static. However, I really believe you wantinline extern Scene* active = new Scene();.Rustthe compiler with default settings will only allow read-only variables to have static lifetime.A.handScene.h(that I bet is the reason ofC2653: Scene is not a class or namespace name). Whatever is read first, reads the other which then tries to read the first but fails (due to the#pragma once). One of these two#includes has to be removed (and replaced by a forward declaration in case). Please, have a look at this: SO: C++ circular header includesconstkeyword.