错误:无法从节点*转换为节点 c++?
Error: cannot convert from node* to node c++?
我是 c++ 的初学者,我正在为学校项目创建自己的 InterlacedList 类,并且我创建了一个 Node 类:
#include "Node.h"
#include "Student.h"
#include <string>
Node::Node()
{
this->student = nullptr;
this->nextName = nullptr;
this->nextYear = nullptr;
this->nextGrade = nullptr;
}
Student Node::getStudent()
{
return this->student;
}
Node Node::getNextName()
{
return this->nextName;
}
Node Node::getNextYear()
{
return this->nextYear;
}
Node Node::getNextGrade()
{
return this->nextGrade;
}
哪个编译很好。
但是在我的隔行列表类中:
#include "InterlacedList.h"
#include "Node.h"
#include "Student.h"
InterlacedList::InterlacedList()
{
this->head = nullptr;
this->tail = nullptr;
}
Node InterlacedList::getHead()
{
return this->head;
}
Node InterlacedList::getTail()
{
return this->tail;
}
我收到此错误: 无法将"((模型::隔行列表*)this)->模型::隔行列表::头"从"模型::节点*"转换为"模型::节点">
和
错误:无法将"((模型::隔行扫描列表*)this)->模型::隔行列表::尾部"从"模型::节点*"转换为"模型::节点">
我的理解是它应该像 Node 类获取者一样工作。 请帮忙。
查看您的InterlacedList::InterlacedList()
,似乎head
和tail
都被定义为指针。您的getHead()
返回一个 Node,而不是指向 Node 的指针,这就是弹出错误的原因:您忘记取消引用它。
您可以更改函数,使其返回指针(不要忘记更改头文件中的类定义):
Node* InterlacedList::getHead()
或返回取消引用的对象:
return *(this->head);
但请务必在执行后者之前检查 nullPTR。此外,如果这样做,最好返回对 Node 的引用,而不是返回副本。
看看Node
的定义。你忘了在问题中展示它,但我的水晶球告诉我它看起来像这样:
class Node { // something ... Node* head; Node* tail; // something ... };
特别注意Node::head
(和Node::tail
)的类型。类型为Node*
。这意味着指向Node
的指针。
接下来看看getHead
(和getTail
)的声明:
Node InterlacedList::getHead()
特别注意函数的返回类型。这是Node
.这与Node*
不同。此返回类型意味着该函数返回一个新的Node
对象。它不返回指针。
接下来看看getHead
(和getTail
)的定义:
return this->head;
好吧,您正在尝试从应该返回Node
的函数返回Node*
。指针(通常)不能隐式转换为其指针类型。错误很清楚:
could not convert .. from ‘model::Node*’ to ‘model::Node’
解决方案:返回类型与函数的返回类型匹配的对象。一种可能的解决方案是间接指针:
return *this->head;
另一种方法是更改函数的返回类型以匹配要返回的指针。考虑您尝试实施的解决方案。
- 如何将节点 V8 字符串转换为 C++ 字符串
- 没有从"树节点"到"树节点*"的可行转换
- 将节点数组(可变长度)转换为常量浮点**以调用opencv.calcHist
- 正在初始化:无法从'_Ty*'转换为 List<int,std::分配器<节点<T>>>:节点*
- 错误:无法从节点*转换为节点 c++?
- 指针取消引用:无法将“节点**”转换为“节点*”
- 无法将参数从"this"转换为节点<T>*
- 用指针 *将C 转换为JavaScript(节点)
- 如何将XML节点数据转换为QT中的字符串
- 如何将节点插件持久<function>转换为本地<function>?
- 在从通用树转换的二进制树中查找节点的父
- 如何使用指针C++将类的析构函数转换为另一个类?类似于单向列表容器类和节点类
- 将阵列转换为节点
- 转换为模板类(节点)
- 如何将列表'node_ptr'转换为我自己的节点类型
- 错误 C2440:"=":无法从"列表节点 *"<T>转换为"列表节点<T> *"
- 在C++中实现图形节点时将 C# 类转换为C++
- 无法在赋值中将"列表节点**"转换为"列表节点*"
- 用于c++的XML解析器,它将XML节点转换为c++对象
- C++中的树实现:无法将节点转换为节点*