[英]A non-static member reference of a variable inside a class' static function
I want to take the OpenGL functionality from the main source file to a separate class. 我想将OpenGL功能从主要源文件带到一个单独的类。
// Initialize rendering (GLUT and GLEW)
gfxMgr.init(argc, argv);
...
glutReshapeFunc(gfxMgr.resizeWindow);
glutKeyboardFunc(gfxMgr.keyPressed);
glutKeyboardUpFunc(gfxMgr.keyReleased);
The problem started with defining the callback functions within the class' implementation file. 问题始于在类的实现文件中定义回调函数。 I declared them in the header file as static.
我在头文件中将它们声明为静态。
static void init(int , char** );
...
static void drawScene();
static void whenIdle();
Then another problem followed. 然后是另一个问题。 I want to use a non-static boolean fullScreen variable (as declared in the header) in one of the static functions of my implementation file, but the IDE tells me that "a non-static member reference must be relative to a specific object".
我想在实现文件的一个静态函数中使用一个非静态的boolean fullScreen变量(如标头中所声明),但是IDE告诉我“一个非静态成员引用必须相对于特定对象”。 。
void GfxMgr::init(int argc, char** argv)
{
...
if(fullScr) glutFullScreen();
...
}
I don't understand the problem and I don't know what to do. 我不明白问题所在,也不知道该怎么办。 I declared the boolean and a few other variables as static but came up with a bunch of unresolved external symbol errors.
我将布尔值和其他一些变量声明为静态变量,但提出了许多未解决的外部符号错误。
I don't understand the problem
我不明白这个问题
You need to understand what static member functions are. 您需要了解什么是静态成员函数。 See this tutorial for example.
例如,请参阅本教程 。 I also recommend the previous one about static member variables.
我还推荐了有关静态成员变量的上一篇。
In short, static member functions don't know anything about instances of the class. 简而言之,静态成员函数对类的实例一无所知。 Non-static member variables are bound to an instance of the class.
非静态成员变量绑定到该类的实例。 Therefore non-static member variables are unavailable to static member functions.
因此,非静态成员变量不适用于静态成员函数。
and I don't know what to do.
而且我不知道该怎么办。
What you should do depends on what your class and it's functions are supposed to do. 您应该做什么取决于您的类及其功能。
C callbacks cannot be member functions. C回调不能是成员函数。 Therefore they cannot depend on state of an instance (except a global instance, see this tutorial )
因此,它们不能依赖实例的状态(全局实例除外,请参阅本教程 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.