簡體   English   中英

無法將“ this”指針從“ const Node”轉換為“ Node&”

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

我真的不知道為什么會出現這些錯誤:

  • 錯誤1錯誤C2662:'無效節點:: setInfo(const類型&)':無法將'this'指針從'const Node'轉換為'Node&'
  • 錯誤2錯誤C2662:'無效節點:: setLink(節點*)':無法轉換
    從'const Node'到'Node&'的'this'指針

這是我正在做的程序。

頭文件:

#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可以使您免於其他問題的otherNode 看來您的復制構造函數實際上是在將“新”節點復制到源節點,而不是相反。

抓緊我以前的答案,因為它很遙遠。
我有一個編譯器,我看到發生了什么事。

在復制構造函數中,您要傳入const引用,然后嘗試修改該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.
}

但是,如果這是您的賦值運算符,那么為什么要有一個? 您要做的只是編譯器生成的版本將要做的事情,那就是對所有成員進行淺表復制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM