我有这个错误:
Undefined symbols for architecture x86_64:
"my::Queue<int>::Queue()", referenced from:
_main in ccdwI88X.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
代码'main.cpp':
#include "Queue.hpp"
int main()
{
my::Queue<int> myqueue;
return 0;
}
'Queue.hpp':
#ifndef QUEUE_HH__
#define QUEUE_HH__
namespace my
{
template <typename T>
class Queue
{
public:
Queue();
};
}
#endif
和'Queue.cpp':
#include "Queue.hpp"
template <typename T>
my::Queue<T>::Queue()
{
}
答案 0 :(得分:5)
这里贴出的答案是:https://stackoverflow.com/a/312402/700926是我认为你需要的。
如果我将Queue.cpp
文件编辑为:
#include "Queue.hpp"
template <typename T>
my::Queue<T>::Queue()
{
}
template class my::Queue<int>;
..编译得很好。
详细说明请参阅我刚才提到的网址。
答案 1 :(得分:1)
使用模板时最简单,最安全的做法是将类函数定义(实现)放在.hpp
文件中,而不是放在单独的.cpp
文件中。
所有细节也在这里:http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12