aar*_*acy 2 c++ linker templates class
我不是一个经验丰富的C++程序员,我在编译时遇到问题.我有一个使用模板的Heap类:
template <class T>
class Heap
{
public:
Heap(const vector<T>& values);
private:
vector<T> d;
// etc.
};
Run Code Online (Sandbox Code Playgroud)
然后在一个单独的实现文件中:
template <class T>
Heap<T>::Heap(const vector<T>& values)
{
d = values;
for (unsigned int i = d.size()-1; i > 0; i--) Heapify(ParentIndex(i));
}
// ... more implementation code ...
Run Code Online (Sandbox Code Playgroud)
最后一个main.cc文件:
int main (int argc, char *argv[])
{
vector<int> in;
unsigned int i;
while (cin >> i) in.push_back(i);
Heap<int> h = Heap<int>(in);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到这些编译错误:
g++ -Wall -I/opt/local/include -c -o main.o main.cc
g++ -Wall -I/opt/local/include -c -o heap.o heap.cc
g++ -Wall -o heap main.o heap.o
Undefined symbols:
"Heap<int>::Heap(std::vector<int, std::allocator<int> > const&)", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [heap] Error 1
Run Code Online (Sandbox Code Playgroud)
为什么这不编译?我认为链接器说它找不到构造函数,但我知道它创建了目标文件.