未定义的引用C++队列模板处理字符串

Undefined Reference C++ Queue Template Handling Strings

本文关键字:处理 字符串 队列 引用 C++ 未定义      更新时间:2023-10-16

我正在做家庭作业,我必须编写一个模板,然后将模板实现到驱动程序中。 不幸的是,我在编译时收到错误。 我刚刚开始学习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 to LinkedQueue, std::allocator>>::number'/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:/tmp/ccdvvLpF.o: 错误 reloc 地址 0x66 在 '.text$_ZN11LinkedQueueISsE7enqueueERKSs[LinkedQueue, std::allocator>>::enqueue(std::basic_string, std::allocator> const&)]'收集2:LD 返回 1 个退出状态

谢谢。

从未

定义static int number。在类外部添加以下定义。

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

static成员是特殊的,类内部static int number只是一个声明,而不是一个定义。这样做的原因是,标头通常包含在多个翻译单元中,因此C++强制您在类外部定义它们,通常在 cpp 文件中,这样您就不会收到多个定义错误。