저는 숙련 된 C++ 프로그래머가 아니며 컴파일하는 데 문제가 있습니다.템플릿이있는 C++ 클래스 컴파일 오류
template <class T>
class Heap
{
public:
Heap(const vector<T>& values);
private:
vector<T> d;
// etc.
};
그리고 별도의 실행 파일 :
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 ...
그리고 마지막으로 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;
}
나는 나는 템플릿을 사용하는 힙 클래스를 가지고 다음과 같은 컴파일 오류가 발생합니다.
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
왜 컴파일되지 않습니까? 링커가 생성자를 찾을 수 없다고 말하는 것 같지만 오브젝트 파일을 만들었다는 것을 알고 있습니다.
힙 생성자가 별도의 파일에 정의되어 있습니까? 템플릿으로는 그렇게 잘 작동하지 않습니다. –