sách gpt4 ai đã đi

c++ - ' =' : cannot convert from ' CircularDoubleDirectedList::节点 *' to ' 节点 *'

In lại 作者:行者123 更新时间:2023-11-30 02:39:22 30 4
mua khóa gpt4 Nike

我有 Node* current ,我在其中存储指向列表“顶部”当前节点的指针。当我将一个新节点设置为当前节点时,出现错误:

'=' : cannot convert from 'CircularDoubleDirectedList::Node *' to 'Node *'
while compiling class template member function 'void CircularDoubleDirectedList::addAtCurrent(const T &)' with [ T=int ]

这是带有//Problem 注释的三行,如果把它们去掉一切正常,就会产生这些错误。

#include "ICircularDoubleDirectedList.h"

template class CircularDoubleDirectedList;
class Node;

template
class CircularDoubleDirectedList :
public ICircularDoubleDirectedList{
công cộng:
//Variables
Node* current;
int nrOfElements;
direction currentDirection;

//Functions
CircularDoubleDirectedList();
~CircularDoubleDirectedList();
void addAtCurrent(const T& element) override;

riêng tư:
class Node
{
công cộng:
T data;
Node* forward;
Node* backward;

Node(const T& element);
};

};
template
void CircularDoubleDirectedList::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
}

1 Câu trả lời

当你转发声明Nút因为不在这里上课:

template  class CircularDoubleDirectedList;
class Node;

那是声明一个类型 Nút在全局命名空间中。是::Node .然后,在你的类声明中,hiện hành接受那个类型:

template 
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList
{
công cộng:
Node* current; // this is a pointer to ::Node.
};

然后您提供 CircularDoubleDirectedList::Node 的声明.这与 ::Node 类型KHÔNG相同.它还首先通过名称解析规则进行查找。所以在这里:

template 
void CircularDoubleDirectedList::addAtCurrent(const T& element){
Node* newNode = new Node(element); // newNode is a pointer to
// CircularDoubleDirectedList::Node

Nhưnghiện hành是指向仍然不完整类型的指针 ::Node .因此出现错误 - 您无意中创建了名为 Nút của hai 类型.

如果你要转发声明Nút ,你必须在类里面做这件事:

template 
class CircularDoubleDirectedList
: public ICircularDoubleDirectedList
{
class Node; // NOW it's CircularDoubleDirectedList::Node
};

关于c++ - ' =' : cannot convert from ' CircularDoubleDirectedList::节点 *' to ' 节点 *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868972/

30 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com