2014-05-01 94 views
0

我實在想不通,爲什麼我得到這些錯誤:無法轉換從「常量節點」「這個」指針「節點」

  • 錯誤1個錯誤C2662:「無效節點:: setInfo(const型&)」 :不能轉換 'const的節點這個' 從指針'到 '節點&'
  • 錯誤2錯誤C2662:「無效節點:: setLink(節點*):不能轉換
    'this'從'const 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; 
} 

回答

0

抓了我以前的答案,因爲它是路要走。
我有一個編譯器,我看到發生了什麼。

在您的副本構造函數中,傳入一個const引用,然後嘗試修改該const引用。

相反,它應該像

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

非常感謝你,它現在有效。我找不到另一種寫法。 – TheSpider

+0

@ user3593832 - 如果這是它所要做的,則不需要您編寫複製構造函數。看到我對你任務操作員的回答。 – PaulMcKenzie

1

的問題是,你正試圖修改常量對象。你的構造函數聲明爲

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 

在這種情況下,我認爲,正確的聲明otherNodeconst從另一個問題,節省您的。看起來你的拷貝構造函數實際上是將你的'新'節點拷貝到源節點中,而不是相反。

+0

我意識到我無法修改一個const對象,並且我傳遞它的方式有些問題。感謝你的回答。 – TheSpider

0

還有一個問題。您的賦值運算符不返回節點引用。它不會返回任何東西。因此,如果調用複製分配,您的程序將調用未定義的行爲。

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

但是,如果這是您的賦值運算符,那麼爲什麼有一個?你所做的只是編譯器生成的版本將做的事情,這只是做一個所有成員的淺表副本。

+0

對不起。我在我的代碼中修復了它。作者賦值運算符不一定是編碼人員,但我必須將其用作ref,因爲它是我的任務的一部分。 – TheSpider