简体   繁体   中英

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).

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 . As your operator< is not marked const , this fails.

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

The standard library expects comparisons to be logically 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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