这是我的头文件:
class MapObject: public ScreenObject {
static float xoffset, yoffset;
public:
static float Getxoffset() {
return xoffset;
}
};
#endif // MAPOBJECT_H
Run Code Online (Sandbox Code Playgroud)
但是在线返回xoffset; 我得到以下错误:未定义引用`MapObject :: xoffset'为什么?
把它放在一个源文件中(看起来很像MapObject.cpp)
#include "MapObject.h"
float MapObject::xoffset = 0;
float MapObject::yoffset = 0;
//... the rest of your MapObject code here...
Run Code Online (Sandbox Code Playgroud)
在C++中,非const static成员必须在类定义中声明并使用全局作用域定义,以正确地为链接器提供引用的内容.