views:

38

answers:

3

Hi All, I have begun writing some code for a library I need. The following code gives me an error

class node {
public:
    node() { }
    node(const node&);
    ~node() { }

    luint getID() { return this->ID; }
    node& operator=(const node&);
protected:
    luint ID;
    std::vector<node*> neighbors;
};
node::node( const node& inNode) {
    *this = inNode;
}

node& node::operator=(const node& inNode) {
    ID = inNode.getID();
}

which is the following:

graph.cpp: In member function 'node& node::operator=(const node&)': graph.cpp:16: error: passing 'const node' as 'this' argument of 'luint node::getID()' discards qualifiers

Did I do anything wrong with the code?

Thanks,

+3  A: 

Your inNode is declared to be const, which means you can only invoke const member functions on it. You'll have to add the const modifier to getID to tell the compiler that it won't modify the object:

luint getID() const { return this->ID; }
casablanca
+1 beat my by 5 seconds :-)
Mario The Spoon
+1 I'm typing too slow today :).
JoshD
A: 

You need to make getID() const.

kotlinski
+1  A: 

In your operator= function, inNode is constant. The function getID is not constant, so calling it is discarding the constness of inNode. Just make getID const:

luint getID() const { return this->ID; }
JoshD