gpt4 book ai didi

C++ 从此处实例化时出现 std::set 错误

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

我发现了一堆关于“从这里实例化”问题的线索。他们似乎都是创建忘记默认构造函数的人。我认为我的问题是不同的(但是我是 C++ 的新手,它可能是同一个问题的轻微变化,我只是不知道如何实现解决方案)。

我正在尝试插入一个集合,显然它是从那里实例化的。它正在抛出一个错误。

class Node{
public:
bool operator <(const Node& other){
return id < other.id;
}
class Graph {
public:
int poner;
map<string, Node> nodeMap;
set<Node> reachables;


void DepthFirstSearch(Node node){
reachables.clear(); //fine at this point
poner = 0;
DFS(node);
}


private:
void DFS(Node node){
reachables.insert(node); //instantiated from here
}

};


Node.h:131:25: instantiated from here
c:\..... errir: passing 'const Node' as 'this' argument of 'bool Node::operator<(const Node&)' discards qualifiers [-fpermissive]

我们将不胜感激任何帮助。

最佳答案

有些东西试图比较 const Nodeconst Node .作为您的operator<未标记 const , 这失败了。

operator<(const Node& other) const {}
^^^^^

标准库期望比较在逻辑上是 const .如果他们真的不能const , 使用 mutable隐藏运算符(operator)正在进行变异,但要确保从外部看不到它。

关于错误信息:instantiated from here实际上只是意味着这段代码负责实例化发生错误的模板。这不是真正的错误,而是实例化回溯的一部分。 真正的 错误通常(在 gcc 中)包含在单词 error 之后。 ,听起来很明显。

关于C++ 从此处实例化时出现 std::set 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13575405/

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