こんにちは、次のコードで未定義の参照エラーが発生しています。
class Helloworld{
public:
static int x;
void foo();
};
void Helloworld::foo(){
Helloworld::x = 10;
};
関数はいらないstatic foo()。staticクラスの非staticメソッドでクラスの変数にアクセスするにはどうすればよいですか?
staticfoo()関数いらない
はクラス内で静的ではなく、クラスfoo()の変数にアクセスするために作成する必要はありません。staticstatic
必要なことは、静的メンバー変数の定義を提供することだけです。
class Helloworld {
public:
static int x;
void foo();
};
int Helloworld::x = 0; // Or whatever is the most appropriate value
// for initializing x. Notice, that the
// initializer is not required: if absent,
// x will be zero-initialized.
void Helloworld::foo() {
Helloworld::x = 10;
};