0

私は宿題に取り組んでおり、テンプレートを作成してから、そのテンプレートをドライバーに実装する必要があります。残念ながら、コンパイル時にエラーが発生します。私はC++のより複雑な側面を学び始めたばかりであり、この問題を解決する方法がわかりません。

/tmp/ccdvvLpF.o:main.cpp:(.text $ _ZN11LinkedQueueISsE7enqueueERKSs [LinkedQueue、std :: allocator >> :: enqueue(std :: basic_string、std :: allocator> const&)] + 0x4a):LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::number' /tmp/ccdvvLpF.o:main.cpp:(.text$_ZN11LinkedQueueISsE7enqueueERKSs[LinkedQueue<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::enqueue(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x66): undefined reference toLinkedQueueへの未定義の参照、std :: allocator >> :: number'/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: /tmp/ccdvvLpF.o:セクション `.text $ _ZN11LinkedQueueISsE7enqueueERKSs [LinkedQueue、std :: allocator >> :: enqueue(std :: basic_string、std :: allocator> const&)]'の不正な再配置アドレス0x66終了ステータス

ありがとう。

4

1 に答える 1

3

static int number定義されることはありません。クラスの外に次の定義を追加します。

template <class T> int LinkedQueue<T>::number = 0;

staticメンバーは特別ですstatic int number。クラス内は宣言であり、定義ではありません。これは、ヘッダーが通常複数の翻訳単位に含まれているためです。C++ では、複数の定義エラーが発生しないように、通常は cpp ファイルでクラスの外でそれらを定義する必要があります。

于 2012-10-04T21:47:19.437 に答える