中級 › staticメンバ変数の undefined reference の原因 › パターン5
staticメンバ変数の undefined reference の原因
クラス内でstaticメンバ変数を宣言しただけで、クラスの外側での実体定義を書き忘れたときにリンクの段階で出るエラーです。
1 #include <iostream> 2 class Session { 3 public: 4 static int active; 5 Session() { active++; } 6 }; 7 ________________________ ^ 8 int main() { 9 Session a, b; 10 std::cout << Session::active << std::endl; 11 return 0; 12 }
undefined reference to `Session::active'
空欄を埋めてコンパイル・実行を通す
実体定義を忘れるとコンパイルは通ってもリンクの段階で失敗します。
次の問題