简体   繁体   English

无法将“ this”指针从“ const Node”转换为“ Node&”

[英]Cannot convert 'this' pointer from 'const Node' to 'Node &'

I really can't figure out why I'm getting these errors: 我真的不知道为什么会出现这些错误:

  • Error 1 error C2662: 'void Node::setInfo(const Type &)' : cannot convert 'this' pointer from 'const Node' to 'Node &' 错误1错误C2662:'无效节点:: setInfo(const类型&)':无法将'this'指针从'const Node'转换为'Node&'
  • Error 2 error C2662: 'void Node::setLink(Node *)' : cannot convert 错误2错误C2662:'无效节点:: setLink(节点*)':无法转换
    'this' pointer from 'const Node' to 'Node &' 从'const Node'到'Node&'的'this'指针

Here's the program I'm doing. 这是我正在做的程序。

Header File: 头文件:

#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;
}

Main File: 主文件:

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;
}

The problem is that you are attempting to modify a constant object. 问题是您正在尝试修改常量对象。 Your constructor declaration is 您的构造函数声明为

template <class Type> Node<Type>::Node(const Node<Type> &otherNode)

The const means that you cannot modify the otherNode object. const表示您不能修改otherNode对象。 You can only call methods on otherNode that are marked as const . 您只能在otherNode上调用标记为const In your body you attempt to modify the otherNode object: 在您的体内,您尝试修改otherNode对象:

otherNode.setInfo(info); //ERROR 1
otherNode.setLink(link); // ERROR 2

In this case, I believe that properly declaring otherNode as const is saving you from another issue. 在这种情况下,我相信正确地将otherNode声明为const可以使您免于其他问题的otherNode It looks like your copy constructor is actually copying your 'new' node into the source node, rather than the other way around. 看来您的复制构造函数实际上是在将“新”节点复制到源节点,而不是相反。

Scratching out my previous answer, because it was way off. 抓紧我以前的答案,因为它很遥远。
I got a compiler and I see what's happening. 我有一个编译器,我看到发生了什么事。

In your copy constructor, you are passing in a const reference, and then attempting to modify that const reference. 在复制构造函数中,您要传入const引用,然后尝试修改该const引用。

Instead it should look like 相反,它应该看起来像

template <class Type> Node<Type>::Node(const Node<Type> &otherNode)
{
    this->setInfo(otherNode.info); 
    this->setLink(otherNode.link);
}

There is a further problem. 还有一个问题。 Your assignment operator doesn't return a Node reference. 您的赋值运算符不会返回Node引用。 It doesn't return anything. 它不返回任何东西。 Therefore your program invokes undefined behavior if a copy-assignment is invoked. 因此,如果调用了复制分配,则程序将调用未定义的行为。

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.
}

However, if this is your assignment operator, then why have one? 但是,如果这是您的赋值运算符,那么为什么要有一个? All you're doing is what the compiler generated version will do, and that is just do a shallow copy of all the members. 您要做的只是编译器生成的版本将要做的事情,那就是对所有成员进行浅表复制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C2664无法将参数1从&#39;const char *&#39;转换为&#39;Node *&#39; - C2664 cannot convert argument 1 from 'const char *' to 'Node*' 无法将“ this”指针从“ const Bullet”转换为“ Bullet&” - Cannot convert 'this' pointer from 'const Bullet' to 'Bullet &' 无法将“ this”指针从“ const CDrawnLabel”转换为“ CDrawnLabel&” - cannot convert 'this' pointer from 'const CDrawnLabel' to 'CDrawnLabel &' &#39;=&#39;:无法从&#39;CircularDoubleDirectedList转换 <int> :: Node *&#39;to&#39;Node *&#39; - '=' : cannot convert from 'CircularDoubleDirectedList<int>::Node *' to 'Node *' 无法将“ this”指针从“ const Foo”转换为“ Foo&”错误 - Cannot convert 'this' pointer from 'const Foo' to 'Foo&' error 无法从“常量列表”转换“this”指针<t> '到'列表<t> &amp;'</t></t> - cannot convert 'this' pointer from 'const List<T>' to 'List<T> &' 无法将“this”指针从“const Line”转换为“Line &amp;”解释? - Cannot convert 'this' pointer from 'const Line' to 'Line &' explanation? 无法从const类转换此指针 <T> 上课 <T> 和 - Cannot convert this pointer from const Class<T> to Class<T>& 不能从&#39;const容器转换&#39;this&#39;指针 <T> &#39;到&#39;容器 <T> &” - cannot convert 'this' pointer from 'const container<T>' to 'container<T> &' 错误C2440:“ <function-style-cast> &#39;:无法从“ LinkedList”转换 <int> :: Node *&#39;到&#39;LinkedListIterator <const T> &#39; - error C2440: '<function-style-cast>' : cannot convert from 'LinkedList<int>::Node *' to 'LinkedListIterator<const T>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM