using namespace std;
class Sample
{ int x;
static int count;
public:
void get();
static void showCount();
};
void Sample::get()
{
cin>>x;
++count;
}
static void Sample::showCount(){
cout<<"Total No. of Objects :"<<count;
}
int main()
{ Sample s1,s2;
s1.get();
s2.get();
Sample::showCount();
return 0;
}
編譯錯誤:[錯誤]不能聲明成員函數「靜態無效樣品:: showCount()」爲有靜態鏈接[-fpermissive]我在程序錯誤計數的對象的總數量與靜態變量
一件事在你的代碼缺少的是count'的'初始化。靜態成員變量(這裏是'count')必須在類中聲明,然後在其外部定義。 –
編譯器定義的默認count = 0。 –