0

次のクラス定義とmain()があります。誰かが私にエラーが発生する理由を教えてもらえますか?

#include <iostream>
#include <list>
using namespace std;

class test
{
protected:
  static list<int> a;
public:
  test()
  {
    a.push_back(150);
  }
  static void send(int c)
  {
    if (c==1)
      cout<<a.front()<<endl;
  }
};

int main()
{
  test c;
  test::send(1);
  return 0;
}

私が得るエラーは次のとおりです。

/tmp/ccre4um4.o: In function `test::test()':
test_static.cpp:(.text._ZN4testC1Ev[test::test()]+0x1b): undefined reference to `test::a'
/tmp/ccre4um4.o: In function `test::send(int)':
test_static.cpp:(.text._ZN4test4sendEi[test::send(int)]+0x12): undefined reference to `test::a'
collect2: ld returned 1 exit status

test :: send(1)の代わりにc.send(1)を使用しても、エラーは同じです。助けてくれてありがとう。

4

2 に答える 2

6

宣言し ましたが、定義test::aしていません。名前空間スコープに定義を追加します。

list<int> test::a;
于 2011-05-31T21:38:06.480 に答える
1

aが宣言されていますが、それでも定義する必要があります。 http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12

于 2011-05-31T21:41:03.767 に答える