简体   繁体   English

C ++从此处实例化错误,并带有std :: set

[英]C++ instantiated from here error with std::set

I found a bunch of threads about the "instantiated from here" problem. 我发现了一堆关于“从这里实例化”问题的线索。 They all seemed to be people who created forgot a default constructor. 他们似乎都是创建了忘记默认构造函数的人。 I think my problem is different (however I am new to C++ and it could be a slight variation on the same problem and I just don't know how to implement the solution). 我认为我的问题是不同的(但是,我是C ++的新手,在同一问题上可能稍有不同,而我只是不知道如何实现该解决方案)。

I am trying to insert into a set and apparently it is being instantiated from there. 我正在尝试插入集合,显然是从那里实例化的。 And it is throwing an error. 而且它抛出一个错误。

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]

Any help is always appreciated. 任何帮助总是很感激。

Somewhere something tries to compare a const Node with a const Node . 在某个地方,有人试图将const Nodeconst Node进行比较。 As your operator< is not marked const , this fails. 由于您的operator<未标记为const ,因此失败。

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

The standard library expects comparisons to be logically const . 标准库期望比较在逻辑上是const If they really cannot be const , use mutable to hide that the operator is doing mutation, but make sure that this is really not visible from the outside. 如果它们确实不能为const ,请使用mutable隐藏操作员正在进行突变,但请确保从外部看不到该变量。

On the error message: instantiated from here really just means that this piece of code is responsible for instantiating the template in which the error occurs. 关于错误消息: instantiated from here实际上仅意味着这段代码负责实例化发生错误的模板。 It is not the real error, but part of the instantiation backtrace. 这不是真正的错误,而是实例化回溯的一部分。 The real error is usually (in gcc) contained after the word error , as obvious as that might sound. 真正的错误通常(在gcc中)包含在单词error后面,听起来可能很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM