开发者

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 a开发者_如何学JAVAnything wrong with the code?

Thanks,


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


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


You need to make getID() const.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜