无法'this'指针从'const Node'转换为'Node &'
Cannot convert 'this' pointer from 'const Node' to 'Node &'
我真的不明白为什么会出现这些错误:
- 错误1错误C2662:"void Node::setInfo(const Type&(":无法将"this"指针从"const Node"转换为'节点&'
- 错误2错误C2662:"void Node::setLink(Node*(":无法转换
"this"指针从"const Node"指向"Node&">
这是我正在做的程序。
头文件:
#pragma once
#include <iostream>
using namespace std;
template <class Type>
class Node
{
private:
Type info;
Node<Type> *link;
public:
// Constructors
Node();
Node(const Type& elem, Node<Type> *ptr);
Node(const Node<Type> &otherNode);
// Destructor
~Node();
// Mutators and Accessors (getters and setters)
void setInfo(const Type& elem);
Type getInfo() const;
void setLink(Node<Type> *ptr);
Node<Type> * getLink() const;
// Overload the assignment operator
const Node<Type> & operator=(const Node<Type>&);
};
template <class Type> Node<Type>::Node()
{
link = NULL;
}
template <class Type> Node<Type>::Node(const Type& elem, Node<Type> *ptr)
{
info = elem;
link = ptr;
}
template <class Type> Node<Type>::Node(const Node<Type> &otherNode)
{
otherNode.setInfo(info); //ERROR 1
otherNode.setLink(link); // ERROR 2
}
template <class Type> Node<Type>::~Node()
{
// fill in this
}
template <class Type> void Node<Type>::setInfo(const Type& elem)
{
info = elem;
}
template <class Type> Type Node<Type>::getInfo() const
{
return info;
}
template <class Type> void Node<Type>::setLink(Node<Type> *ptr)
{
link = ptr;
}
template <class Type> Node<Type> * Node<Type>::getLink() const
{
return link;
}
template <class Type> const Node<Type> & Node<Type>::operator=(const Node<Type>& n)
{
info = n.info;
link = n.link;
}
主文件:
include "Node.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
Node<string> *node1 = new Node<string>();
node1->setInfo("Hello");
Node<string> *node2 = new Node<string>("Hello World!", node1);
Node<string> *node3 = new Node<string>(*node2);
Node<string> *node4 = new Node<string>();
node4->setInfo("Foo Bar");
node4->setLink(node3);
cout << node3->getLink()->getInfo() << endl; // should return "hello world"
system("pause");
return 0;
}
问题是您试图修改常量对象。您的构造函数声明是
template <class Type> Node<Type>::Node(const Node<Type> &otherNode)
const
表示不能修改otherNode
对象。只能调用otherNode
上标记为const
的方法。在您的身体中,您试图修改otherNode
对象:
otherNode.setInfo(info); //ERROR 1
otherNode.setLink(link); // ERROR 2
在这种情况下,我相信正确地将otherNode
声明为const
可以避免出现另一个问题。看起来您的复制构造函数实际上是在将"新"节点复制到源节点中,而不是相反。
删掉我以前的答案,因为它太离谱了。
我有一个编译器,我知道发生了什么。
在复制构造函数中,传入一个常量引用,然后尝试修改该常量引用。
相反,它应该看起来像
template <class Type> Node<Type>::Node(const Node<Type> &otherNode)
{
this->setInfo(otherNode.info);
this->setLink(otherNode.link);
}
还有一个问题。您的赋值运算符没有返回Node引用。它什么也不回。因此,如果调用了副本分配,程序将调用未定义的行为。
template <class Type> const Node<Type> & Node<Type>::operator=(const Node<Type>& n)
{
info = n.info;
link = n.link;
// so what happened to the return value?
// return *this; // You're missing this.
}
然而,如果这是您的分配运算符,那么为什么要有一个呢?您所要做的就是编译器生成的版本将要做的事情,也就是对所有成员进行浅层复制。
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- 在 node.js 中将缓冲区从 C++ 转换为 UTF-8 字符串
- 链表(从 'Node<int>*' 到 'int' 的转换无效 [-fallowive]|)
- 请求从非标量类型"node*"转换为标量类型 "node"
- '=' : 无法从'CircularDoubleDirectedList<int>::Node *'转换为'Node *'
- C++ 中请求从 'node*' 型转换为非标量型'node'
- 错误 C2440:'=':无法从 'Node<ValueType> *' 转换为 'Node<ValueType> *'
- 无法编译C++代码:从'Node*'到'int'的转换无效
- C2664 无法将参数 1 从 'const char *' 转换为 'Node*'
- C2440 '=' 无法从 'int' 转换为 'BST:<int>:Node*
- C2678 二进制"<":未找到采用 'const Node' 类型左操作数的运算符(或没有可接受的转换)
- node.js中使用node-gyp的转换数组
- Node.js 6增加了将Buffer.from()转换为const UInt8*的功能
- Node.js c++绑定错误:'('标记之前期望的构造函数、析构函数或类型转换
- 无法'this'指针从'const Node'转换为'Node &'
- 在C++中将一行转换为Node
- 映射和节点,错误智能感知:不存在从"const std::pair<const int, double>"到"Node"的合适用户定义转换