-5

Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?
Link error using templates

I hava a template class 'MyHeap' in two files 'MyHeap.h' and 'MyHeap.cpp'. Compile in vs10, ok. but when I try to use this class in main, this error happened. I looked around, still can't solve this myself. please help me...

Error Info >>

MyHeap<int> minHeap(MyHeap<int>::MaxHeap);

Error 1 error LNK2001: unresolved external symbol "public: __thiscall MyHeap::MyHeap(enum MyHeap::HeapType)" (??0?$MyHeap@H@@QAE@W4HeapType@0@@Z) D:\文档\Visual Studio 2010\Projects\C++\ConsoleCPP_Trivia\ConsoleCPP_Trivia\program.obj Error 2 error LNK1120: 1 unresolved externals D:\文档\Visual Studio 2010\Projects\C++\ConsoleCPP_Trivia\Debug\ConsoleCPP_Trivia.exe

MyHeap.h:
-----------------------------------------------------------
#pragma once

#include <vector>
using namespace std;

template <class T>
class MyHeap
{
public:
    static enum HeapType {CustomizedHeap, MaxHeap, MinHeap};

private:
    typedef bool (*COMP_FUNC)(const T&, const T&);
    COMP_FUNC _comp;
    HeapType _heapType;
    vector<T> _data;

public:
    MyHeap(HeapType heap_type = MaxHeap);
    MyHeap(COMP_FUNC compare_function);

    void add(const T& item);    
    T remove();
    bool isEmpty() const;
    void clear();

private:
    bool __compare(const T&, const T&);
};
Community
  • 1
  • 1
tmj
  • 1,143
  • 3
  • 11
  • 15

1 Answers1

3

Templates must be defined in the header files. You cannot separate implementation of template classes/functions in source and header files.

The reason is, that templates are compile-time "feature" of C++ and their implementations must be visible at compile time.

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187