对静态成员函数中静态成员变量的未定义引用
undefined reference to static member variable in a static member function
这是我的头文件:
class MapObject: public ScreenObject {
static float xoffset, yoffset;
public:
static float Getxoffset() {
return xoffset;
}
};
#endif // MAPOBJECT_H
然而,在线上返回xoffset;我得到以下错误:对"MapObject::xoffset"的未定义引用为什么?
将其放入源文件(看起来是MapObject.cpp
)
#include "MapObject.h"
float MapObject::xoffset = 0;
float MapObject::yoffset = 0;
//... the rest of your MapObject code here...
在C++中,非常量static
成员必须在类定义中声明,并且定义的具有全局作用域,才能正确地为链接器提供可引用的内容。
你的MapObject.cpp
:中必须有这样的东西
float MapObject::xoffset = 0.0f;
float MapObject::yoffset = 0.0f;
通过这种方式,您可以定义和初始化它们。
相关文章:
- Qt 静态库未定义引用
- CMake 不链接 C 和 C++ 静态库(未定义对函数的引用)
- thread_local静态成员模板定义:初始化失败,GCC
- 对类的静态成员的未定义引用
- 我正在尝试在我的类中创建一个静态成员,但编译器警告我它是未定义的
- 我可以在将项目编译为静态库(未定义的引用)时在代码中使用 QImage 吗?
- 为什么静态成员函数定义不能有关键字"static"?
- MSYS2静态QT未定义的参考问题
- 如果静态成员未初始化并且成员类型是类本身,该怎么办?
- 别名 私有子结构中的静态成员的定义
- 对静态 constexpr 成员的未定义引用仅由值使用
- 静态 constexpr 模板成员在专用时提供未定义的引用
- 对受保护静态成员的未定义引用.我该如何解决
- 类c++中的静态成员未定义引用
- 为什么在 linux 信号事件中未定义静态成员的使用
- 静态成员未被更新
- 静态变量-未定义的引用
- 模板静态var =未定义的引用
- 无法访问静态成员 ->未解析的外部符号 i
- 模板和静态对象(未定义的引用和必需的引用)