【发布时间】:2015-07-03 00:19:21
【问题描述】:
我的编译器找不到嵌套类的构造函数的定义。
我的嵌套类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
【问题讨论】:
-
我认为问题可能是您转发声明的
Node并在标头的模板特化中使用Node。您可能需要在此处自行导入Node的头文件。 -
为什么 Node 类是模板化的?
-
如何导入头文件本身? ifndef 不应该停止吗?我会在哪里导入它?我尝试在包含后执行此操作,但没有任何反应。
标签: c++