gpt4 book ai didi

c++ - 实现模板,我有问题 "matching function definitions to existing declarations"

转载 作者:行者123 更新时间:2023-11-28 07:15:32 25 4
gpt4 key购买 nike

我觉得我正在服用疯狂的药片。语法是我以前用过的(取 self 类的示例类)并且没有任何问题。唯一的区别是我现在使用的是 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;
}

最佳答案

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

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

参见 live样本

关于c++ - 实现模板,我有问题 "matching function definitions to existing declarations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20294926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com