C++ 静态函数中竟然不能使用静态成员?!

来源:百度知道 编辑:UC知道 时间:2024/06/15 06:26:20
类声明:
class Boy
{
public:
static void Goout(int i);
private:
char num;
Boy *next;
static Boy *Lastnode;
static int m,n,key;
};
这是定义的函数:
void Boy::Goout(int i)
{
Boy *die;
int k;
die=Lastnode;
if(i==0)
k = m;
else
k = key;
for(int j=1;j<=k;j++)
{
die=die->next;
if(j==k)
die->num='@';
}
}
但是在编译过程中对k = m;k = key;2句话出现了:
C:\Documents and Settings\USER\桌面\Josephus\Debug\Josephus.o(.text+0x4b): In function `ZN3Boy9CreatListEii':
C:\Documents and Settings\USER\桌面\Josephus\Josephus.cpp:28: undefined reference to `Boy::key'
C:\Documents and Settings\USER\桌面\Josephus\Debug\Josephus.o(.text+0x75): In function `ZN3Boy5GooutEi':
C:\Documents and Settings\USER\桌面\Josephus\Josephus.cpp:38: undefined refere

也不知道你是怎么写的

静态成员函数中绝对是可以 使用静态数据成员的
不知道你的程序 cpp文件中有没有以下几行
Boy *Boy::Lastnode=0;
int Boy::m=0;
int Boy::n=0;
int Boy::key=0;

静态成员需要赋初值的.