可能重复:
C++:对静态类成员的未定义引用
我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)
bil*_*llz 42
您只声明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;
}
Run Code Online (Sandbox Code Playgroud)
你的init()函数也应返回一个值或设置为void.
Ola*_*che 16
你已经A::i在课堂上宣布了,但你还没有定义它.您必须在之后添加定义class A
class A {
public:
static int i;
...
};
int A::i;
Run Code Online (Sandbox Code Playgroud)