【发布时间】:2013-06-26 05:47:01
【问题描述】:
我不明白为什么静态私有变量在我的代码中不起作用!代码如下:
#include<iostream>
#include<string>
using namespace std;
class KittyCat{
private:
int a;
string t;
static int count;
public:
KittyCat(){
a=0;
t="NULL";
count++;
}
void set(int i, string m){
a=i;
t=m;
}
void show(){
cout << "A is: "<< a <<" T is: " << t <<"\n\n";
}
void totalCount(){
cout <<"Total Counts: "<< count <<"\n\n";
}
};
void main(){
KittyCat tech, review, article, photo, video;
tech.set(10, "Technology");
review.set(85, "Reviews");
article.set(54, "Articles");
article.show();
article.totalCount();
}
有什么想法吗?
【问题讨论】:
-
与其说(或除此之外)“不工作”,不如说您期望什么行为,以及您观察到什么行为。
-
@Navin,不,'count'默认初始化为0。
-
你已经声明
count,但你从来没有定义它。在KittCat的定义之后,main之前添加一行int KittyCat::count;。 -
@Navin,第二次,静态变量默认初始化为0!
-
@user1764961 这就是我所说的......我提到的“初始化”这个词是错误的,它应该是“定义”。就像杰瑞的例子一样。