Search code examples
c++constantsqualifiers

c++ qualifier error


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,


Solution

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