我编译器无法找到嵌套类的构造函数的定义。
我的嵌套类Node在中间,构造函数在最后。
错误:
错误C2244:' CircularDoubleDirectedList :: Node :: Node' :无法 将函数定义与现有声明匹配请参阅 宣告' CircularDoubleDirectedList :: Node :: Node'
定义
' CircularDoubleDirectedList :: Node :: Node(const T&)'
现有声明
' CircularDoubleDirectedList :: Node :: Node(const T&)'
代码:
#ifndef CIRCULARDOUBLEDIRECTEDLIST_H
#define CIRCULARDOUBLEDIRECTEDLIST_H
#include "ICircularDoubleDirectedList.h"
template <typename T> class CircularDoubleDirectedList;
template <typename T> class Node;
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variabels
Node<T>* current;
int nrOfElements;
direction currentDirection;
//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;
private:
template <typename T>
class Node
{
public:
T data;
Node<T>* forward;
Node<T>* backward;
Node(const T& element);// The constructor
};
};
template <typename T>
CircularDoubleDirectedList<T>::CircularDoubleDirectedList(){
this->nrOfElements = 0;
this->current = nullptr;
this->currentDirection = FORWARD;
}
template <typename T>
CircularDoubleDirectedList<T>::~CircularDoubleDirectedList(){
//TODO: Destroy all nodes
}
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node<T>* newNode = new Node<T>(element);
newNode->data = element;
if (this->nrOfElements == 0){
newNode->forward = newNode;
newNode->backward = newNode;
}
else{
//this->current->forward = newNode;
//this->current->forward->backward = newNode;
}
//this->current = newNode;
}
template <typename T>
CircularDoubleDirectedList<T>::Node<T>::Node(const T& element){
this->data = element;
}
#endif
答案 0 :(得分:1)
首先,前向声明的template <typename T> class Node;
与CircularDoubleDirectedList::Node
不同 - 前者是全局类模板,后者是嵌套类。
其次,您不需要将CircularDoubleDirectedList::Node
声明为模板(如果这样做,则必须为其使用另一个模板参数名称,而不是T
)。但据我所知,对于这种情况,你应该把它设为非模板,所以:
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
private:
class Node
{
public:
T data;
Node* forward;
Node* backward;
Node(const T& element);// The constructor
};
public:
Node* current;
//...
};
template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element){
this->data = element;
}
答案 1 :(得分:0)
您有两个名为Node
的类模板,而实际上您需要一个名为Node
的非模板类。您已向前声明::Node<T>
,并且您拥有嵌套的::CircularDoubleDirectedList<T>::Node<U>
。
如果您真的想要这样,那么您必须在构造函数定义中添加另一个template
关键字:
template <typename T> //because CircularDoubleDirectedList is a template
template <typename U> //because Node is a template
CircularDoubleDirectedList<T>::Node<U>::Node(const T& element) : data(element)
{}
但是,我看不出让Node
成为模板的单一原因。在CircularDoubleDirectedList<T>
内,您是否要使用T
以外的类型的节点?如果没有,请将Node
设为普通的非模板类:
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variabels
Node<T>* current;
int nrOfElements;
direction currentDirection;
//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;
private:
class Node
{
public:
T data;
Node* forward;
Node* backward;
Node(const T& element);// The constructor
};
};
template <typename T>
CircularDoubleDirectedList<T>::Node::Node(const T& element) : data(element)
{}