gpt4 book ai didi

c++限定符错误

转载 作者:行者123 更新时间:2023-11-30 02:11:04 25 4
gpt4 key购买 nike

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

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: In member function 'node& node::operator=(const node&)': graph.cpp:16: error: passing 'const node' as 'this' argument of 'luint node::getID()' discards qualifiers

我的代码有什么问题吗?

谢谢,

最佳答案

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

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

关于c++限定符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3927416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com