zzzbbx
zzzbbx

Reputation: 10141

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,

Upvotes: 1

Views: 755

Answers (3)

JoshD
JoshD

Reputation: 12814

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

Upvotes: 1

Johan Kotlinski
Johan Kotlinski

Reputation: 25749

You need to make getID() const.

Upvotes: 0

casablanca
casablanca

Reputation: 70711

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

Upvotes: 3

Related Questions