我正在尝试测试 Micheal T. Goodrich 等人在“C++ 中的数据结构和算法”中的 SinglyLinked List 示例。我添加了作者省略的一些细节以使其可运行。
代码如下:
#ifndef S_LINKED_LIST
#define S_LINKED_LIST
template <typename E>
class SLinkedList;
template <typename E>
class SNode
{
private:
E elem;
SNode<E> * next;
friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList
{
private:
SNode<E> * head;
public:
SLinkedList();
~SLinkedList();
bool empty() const;
const E& front() const;
void addFront(const E& e);
void removeFront();
};
#endif /*SLinkedList.h*/
实现:
#include "SLinkedList.h"
#include <iostream>
template <typename E>
SLinkedList<E>::SLinkedList():head(NULL){}
template <typename E>
SLinkedList<E>::~SLinkedList()
{while(!empty()) removeFront();}
template <typename E>
bool SLinkedList<E>::empty() const
{return head == NULL;}
template <typename E>
const E& SLinkedList<E>::front() const
{return head->elem;}
template <typename E>
void SLinkedList<E>::addFront(const E& e)
{
SNode<E> * newNode = new SNode<E>;
newNode->elem = e;
newNode->next = head;
head = newNode;
}
template <typename E>
void SLinkedList<E>::removeFront()
{
SNode<E> * old = head;
head = old->next;
delete old;
}/*SLinkedList.cpp*/
测试文件:
#include <iostream>
#include "SLinkedList.h"
int main()
{
SLinkedList<std::string> newlist;
newlist.addFront("MSP");
std::cout << newlist.front();
return 0;
}/*test_slinkedlist.cpp*/
运行 g++ -c SLinkedList.cpp
和 g++ -c test_slinkedlist.cpp
之后
我得到目标文件 SLinkedList.o
和 test_slinkedlist.o
没有错误。
但是当我运行 g++ -o result test_slinkedlist.o SLinkedList.o 时,出现链接器错误:
Undefined symbols for architecture x86_64:
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我花了一天时间调试这个链接器问题,但找不到。我怀疑这很明显。
操作系统:OS X
完整错误信息:
Undefined symbols for architecture x86_64:
"SLinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::addFront(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in test_slinkedlist.o
"SLinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::SLinkedList()", referenced from:
_main in test_slinkedlist.o
"SLinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::~SLinkedList()", referenced from:
_main in test_slinkedlist.o
"SLinkedList<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::front() const", referenced from:
_main in test_slinkedlist.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
最佳答案
Implementation:
你没有说,但我们可以假设实现在 SLinkedList.cpp
中.
Undefined symbols for architecture x86_64:
...
在...
,您省略了错误消息中最重要的部分。但是,我们可以猜测 SLinkedList<std::string>::addFront(...)
是 Unresolved 符号之一。
您的问题是模板方法的主体(即实现)必须出现在它所在的每个编译单元中使用过,并且您将此正文放入单独的 .cpp
中违反了该规则文件,编译器在编译时不可见 test_slinkedlist.cpp
.
这就是为什么模板主体最常包含在 SLinkedList.h
中的原因(注意 .h
,而不是 .cpp
)。另见 this answer .
附言从技术上讲,上面的“必须”是不正确的:您还可以使用显式实例化让编译器生成您需要的模板方法体。但这比将实现放入 .h
更高级一些。文件。
关于c++ - 链接目标文件时链接器命令失败,退出代码为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560700/