0

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.

Chase Roberts
  • 9,082
  • 13
  • 73
  • 131
  • 2
    Error message, please. You posted a small portion of it. – chris Nov 27 '12 at 00:17
  • 5
    "instantiated from here" is not the error. It's diagnostic - when having template error it's helpful to have information about both template definition and use. Do you have `operator<` for `Node`? `std::set` requires comparison operation. – zch Nov 27 '12 at 00:20
  • zch, that is probably my problem... I don't have the – Chase Roberts Nov 27 '12 at 01:08
  • I overloaded the < operator. I am still having the same problem. I took it to one of the TA's in my class and he was just as baffled as I. – Chase Roberts Nov 27 '12 at 17:02

1 Answers1

1

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.

pmr
  • 58,701
  • 10
  • 113
  • 156