'=' : 无法从'CircularDoubleDirectedList<int>::Node *'转换为'Node *'
'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *'
>我有 Node* 当前,我在其中存储指向列表"顶部"当前节点的指针。当我将新节点设置为当前节点时,出现错误:
'=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *'
while compiling class template member function 'void CircularDoubleDirectedList<int>::addAtCurrent(const T &)' with [ T=int ]
如果将它们拿走,则带有//Problem 注释的三行会生成这些错误,一切正常。
#include "ICircularDoubleDirectedList.h"
template <typename T> class CircularDoubleDirectedList;
class Node;
template <typename T>
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList<T>{
public:
//Variables
Node* 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);
};
};
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node* newNode = new Node(element);
newNode->data = element;
if (this->nrOfElements == 0){
newNode->forward = newNode;
newNode->backward = newNode;
}
else{
this->current->forward = newNode; // Problem
this->current->forward->backward = newNode; // Problem
}
this->current = newNode; //Problem
}
当您转发声明Node
在此处的类之外时:
template <typename T> class CircularDoubleDirectedList;
class Node;
即在全局命名空间中声明类型Node
。这是::Node
.然后,在您的类声明中,current
采用该类型:
template <typename T>
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList<T>
{
public:
Node* current; // this is a pointer to ::Node.
};
然后,您提供CircularDoubleDirectedList<T>::Node
声明。这与 ::Node
的类型不同。它还首先通过名称解析规则进行查找。所以在这里:
template <typename T>
void CircularDoubleDirectedList<T>::addAtCurrent(const T& element){
Node* newNode = new Node(element); // newNode is a pointer to
// CircularDoubleDirectedList<T>::Node
但是current
是指向仍然不完整的类型::Node
的指针。因此出现错误 - 您无意中创建了两个名为 Node
的类型。
如果你要向前声明Node
,你必须在类中这样做:
template <typename T>
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList<T>
{
class Node; // NOW it's CircularDoubleDirectedList<T>::Node
};
相关文章:
- node-gyp 在 macOS 上未正确链接库
- EASTL矢量<向量<int>>连续的
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- node-gyp 的先有鸡还是先有蛋的问题:指向依赖项中的头文件
- 在 ubuntu 上安装 node js pulsar 客户端
- 如何使用 Node-addon-API 实现 node-nan 回调
- Node.js fs.open() 在尝试打开 4 个以上的命名管道 (FIFO) 后挂起
- NodeJS:node-gyp 编译与等效的 gcc -lm 选项
- 调用'Node'构造函数是不明确的
- 为什么我得到变量"Node"不是类型名称错误
- 在 node.js 中将缓冲区从 C++ 转换为 UTF-8 字符串
- 如何从 node-ffi 调用 c++ 中以结构向量作为参数的方法?
- 使用 Node.js N-API 调用 C 函数时,不会显示预期的输出
- 给一个图 G 找到此图中的所有节点 blob,以便 node.color 属性 = 特定颜色
- 为什么 const YAML::Node 对象的行为不像 yaml-cpp 中的类似值的对象?
- 无法使用 'const Node *' 类型的右值初始化 'Node *' 类型的变量
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- 用输出参数包装一个c++函数,以便在javascript/node中使用
- 在node-gip binding.gyp文件中,如何根据系统结构(32位、64位)包含不同的库文件
- node.h/node.cpp/nodelist.cpp之间的关系