注册 登录
编程论坛 C++教室

[求助]error LNK2001: unresolved external symbol "private: static int c:

tangofan 发布于 2007-06-27 14:33, 4565 次点击
#include<iostream>
using namespace std;
class c{
public:
void f(){cout<<++x<<endl;}
private:
static int x;
};
int main(){
c c1;
c1.f();
return 0;
}
书上这个简单的程序要求找出错误,我觉得挺正确的呀!运行后出现error LNK2001: unresolved external symbol "private: static int c::x" 请帮帮忙。
12 回复
#2
天下第二刀2007-06-27 14:43
静态成员在先初始化
#3
wfpb2007-06-27 15:21

静态成员变量可以是私有的吗?如果可以,那怎么初始化啊???

#4
天下第二刀2007-06-27 15:26
int c::x =0;

这样不行??
#5
wfpb2007-06-27 15:27

我还一位私有不能访问呢,呵呵,不好意思。

#6
HJin2007-06-27 18:20
以下是引用tangofan在2007-6-27 14:33:26的发言:
#include<iostream>
using namespace std;
class c{
public:
void f(){cout<<++x<<endl;}
private:
static int x;
};
int main(){
c c1;
c1.f();
return 0;
}
书上这个简单的程序要求找出错误,我觉得挺正确的呀!运行后出现error LNK2001: unresolved external symbol "private: static int c::x" 请帮帮忙。

first choice is:

class c{
public:
void f(){cout<<++x<<endl;}
private:
static int x;
};

int c::x = 0;

2dn choice is (if your static int is a constant):

class c{
public:
void f(){cout<<++x<<endl;}
private:
static const int x = 0;
};

int c::x;

[此贴子已经被作者于2007-6-27 18:21:02编辑过]

#7
aipb20072007-06-27 18:29
楼上完全正确!
#8
tangofan2007-06-27 21:53

谢谢各位给的回答!

Thank you!

呵呵最近就要考试了。

#9
野比2007-06-27 21:56

加油哦

#10
tangofan2007-06-27 22:26

等等,我发现一个问题:
class c{
public:
void f(){cout<<++x<<endl;}
private:
static int x;
};

int c::x = 0; 就算没有这句话,程序会默认x的的值为0的。

#11
aipb20072007-06-27 22:31
没有那句话不就和开始你那程序一样了??????????
#12
tangofan2007-06-27 22:36
就是啊,所以我觉得问题不是出在那吧?
#13
aipb20072007-06-27 22:40
但是你都编译了有错误啊,怎么能那样判断呢?
1