实现模板,我遇到了“将函数定义与现有声明匹配”的问题

时间:2013-11-29 23:59:44

标签: c++ templates

我觉得我正在服用疯狂的药片。语法是我之前使用的(取自我班级的示例类)并且没有任何问题。唯一的区别是我现在正在使用Visual Studio 2013。当我尝试构建我的解决方案时,我一直收到这个错误(对于所有方法都会发生这种错误,所以我必须有一些语法错误吗?)...

error C2244: 'node<T>::getItem' : unable to match function definition to an existing declaration
1>          c:\users\milan\documents\visual studio 2013\projects\threadedbst\threadedbst\node.h(19) : see declaration of 'node<T>::getItem'
1>          definition
1>          'T node::getItem(void)'
1>          existing declarations
1>          'T node<T>::getItem(void)'

这是我使用模板制作的简单节点类。

using namespace std;

//---------------------------------------------------------------------------
// node<T> class: 
//   -- 
//
// Assumptions:
//   -- <T> maintains it's own comparable functionality
//---------------------------------------------------------------------------

template <typename T>
class node {

public:
    node(T* value); //constructor
    node(const node<T>&); //copy constructor
    void setFrequency(int); //sets the frequency
    int getFrequency(); //returns the frequency
    T* getItem(); //returns the item

private:
    T* item;
    int frequency;
    node<T>* leftChild;
    node<T>* rightChild;
    bool leftChildThread;
    bool rightChildThread;
};

//-------------------------- Constructor ------------------------------------
template <typename T>
node<T>::node(T* value) {
    item = value;
    frequency = 1;

}

//-------------------------- Copy ------------------------------------
template <typename T>
node<T>::node(const node<T>& copyThis) {
    item = copyThis.value;
    frequency = copyThis.frequency;
}

//-------------------------- setFrequency ------------------------------------
template <typename T>
void node<T>::setFrequency(int num) {
    frequency = num;
}

//-------------------------- getFrequency ------------------------------------
template <typename T>
int node<T>::getFrequency() {
    return frequency;
}

//-------------------------- getItem ------------------------------------
template <typename T>
T* node<T>::getItem() {
    return item;
}

1 个答案:

答案 0 :(得分:1)

C ++不允许自嵌套类型,需要使用指针

template <typename T>
class node {
    .... 
    node<T>* leftChild;  //pointer
    node<T>* rightChild;
    ...
};

参见live示例