对静态变量的未定义引用 [英] Undefined reference to static variable
本文介绍了对静态变量的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
C ++:对静态类成员的未定义引用
我使用MinGW。为什么静态变量不起作用
[链接器错误]未定义引用`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;
}
解决方案
$ c> 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。 / p>
Possible Duplicate:
C++: undefined reference to static class member
I'm using MinGW. Why static variable is not working
[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;
}
解决方案
You only declared A::i
, need to define A::i
before using it.
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;
}
Also your init() function should return a value or set to void.
这篇关于对静态变量的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文