我在努力理解術語靜態方法及其工作方式。這裏我初始化了一個靜態變量「float percentage」。我必須把它轉換爲使用convertToPercent()百分比並將其轉換後,我也要把值,並把價值的someMethod()做一些計算無法理解術語「靜態」
下面是我
.H
class Foo {
private:
static float percentage;
public :
float convertToPercent();
};
的.cpp
float Foo::convertToPercent() {
percentage = (30/100) * 100;
return percentage;
}
static float someMethod() {
//place the static percentage value here after doing convertToPercent() method to do some calculation;
}
但它引發我一個錯誤消息
Undefined symbols for architecture x86_64:
"Foo::percentage", referenced from:
Foo::convertToPercent() in Foo.o
給予幫助的頂點。謝謝
你需要的地方很多來定義它,看到有關未定義符號靜在這裏... – PlasmaHH
'static'關鍵字有很多不同的含義的其他問題的極大數,所以要小心 –
好了感謝。設法已經在Visual Studio中使用Foo.o – user3493435