以下的靜態函數使用一個靜態變量編譯細跟克++:如何在一個結構
struct acounter {
long static counter;
void static create() { //reset or create the counter
counter=0;
}
void static count() { //the counter changes its internal value.
counter=counter + 1;
}
};
int main(int argc, char *argv[]){ //compiles and executes!
//do some random stuff...
return 0;
}
的問題是:一旦予添加「acounter ::創建();」或「acounter :: count();」到主循環,我得到一個錯誤:
undefined reference to `acounter::counter'
但我定義「計數器」,甚至初始化它。問題是什麼?
(PS我只能用靜態的功能,因爲我有對付回調後 - 的想法是使用整個結構只在其全球範圍內,而無需創建實例。)
我認爲這是常見的做法,而不是味道的問題,首先放置關鍵字,如「靜態」。 – Chiffa
@Chiffa:好吧,我也會這麼說,但我不能爲所有人說話。我認爲它絕對更具可讀性,而且我從來沒有在「真實」代碼庫中看到過其他任何東西...... –
謝謝,完美的答案 - 一條額外的代碼就完成了。 – Kenobi