【问题标题】:c++ qualifier errorc++限定符错误
【发布时间】:2010-10-13 19:48:07
【问题描述】:

我已经开始为我需要的库编写一些代码。下面的代码给了我一个错误

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

如下:

graph.cpp: 在成员函数'node& 节点::运算符=(常量节点&)': graph.cpp:16:错误:传递'const node'作为'luint'的'this'参数 node::getID()' 丢弃限定符

我的代码有什么问题吗?

谢谢,

【问题讨论】:

标签: c++ constants qualifiers


【解决方案1】:

您的inNode 被声明为const,这意味着您只能在其上调用const 成员函数。您必须将 const 修饰符添加到 getID 以告诉编译器它不会修改对象:

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

【讨论】:

    【解决方案2】:

    在您的 operator= 函数中,inNode 是常量。 getID 函数不是常量,所以调用它就是丢弃 inNode 的常量。只需将 getID 设为 const:

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

    【讨论】:

      【解决方案3】:

      您需要将 getID() 设为常量。

      【讨论】:

        猜你喜欢
        • 2018-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 2012-10-20
        相关资源
        最近更新 更多