Question

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 &'
  • Error 2 error C2662: 'void Node::setLink(Node *)' : cannot convert
    'this' pointer from 'const Node' to 'Node &'

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;
}
Was it helpful?

Solution 2

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.

Instead it should look like

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

OTHER TIPS

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. You can only call methods on otherNode that are marked as const. In your body you attempt to modify the otherNode object:

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. It looks like your copy constructor is actually copying your 'new' node into the source node, rather than the other way around.

There is a further problem. Your assignment operator doesn't return a Node reference. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top