c++ - 对静态变量的 undefined reference

标签 c++

Possible Duplicate:
C++: undefined reference to static class member

我正在使用 MinGW。为什么静态变量不起作用

[Linker error] undefined reference to `A::i' 

#include <windows.h>

    class A { 
        public:     
        static int i;
        static int init(){

            i = 1;  

        }

    };

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil){
    A::i = 0;
    A::init();

    return 0;
}

最佳答案

你只声明了A::i,使用前需要定义A::i

class A  
{ 
public:     
  static int i;
  static void init(){
     i = 1;  
  }
 };

int A::i = 0;

int WINAPI WinMain (HINSTANCE hThisInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpszArgument,
                int nFunsterStil)
{
  A::i = 0;
  A::init();

  return 0;
}

你的 init() 函数也应该返回一个值或设置为 void。

关于c++ - 对静态变量的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14331469/

相关文章:

c++ - cin.get() 获取太多

c++ - 仅计算 "core"QImage 数据(不包括元数据)的 QCryptographicHash

c++ - 防止通过指针调用成员函数

c++ - 检测何时释放键

c++ - 在 QML 中旋转图像时降低 CPU 使用率

c++ - C++ Metro App 教程的编译错误 - 任务继续

c++ - 从字符串中获取键/值对并存储在映射中

c++ - QTreeView 禁止显示根节点

c++ - 在 QML 或 C++ 上设计 UI,BB10

c++ - 调整 SDL2 窗口大小?