Hi there! Im trying to insert two objects (actually 1 object and a an object list) into a map and am getting an error "no matching function for call to std::map<Node, std::...". Anyone able to see what I am doing wrong here?
The only ordering of the Node class I've defined is with the operator< function. Is there a specific function in the node class I am supposed to define for ordering?
Regarding node as not being an iterator - I thought the insert method took 2 parameters - the (key, value) tuple, yes? Where I have overridden the operator< function in the node class, I thought that the insert method would work in this case. But for some reason it does not. Please see below for the operator< function:
1 2 3 4 5 6 7 8 9 10 11 12
// Within the Node.cpp file
bool Node::operator<(const Node &node)const{
if(this->state == node.getState()){
returntrue;
} else {
returnfalse;
}
}
Why are you defining a “less than” operator with an “equals” statement?
As for your insert call,
- you can insert a single “pair” object, but you are trying to supply two arguments
- why are you trying to insert at a point that already exists? (Your slightly contorted logic runs this loop when a value for node is already found in the map.)
Aah, good point with the "less than" operator. I'll fix that up.
What is an example of a "pair" object?
What do you mean by - trying to insert at a point that already exists? Is this referring to the if statement? If so - I am trying to add to the map if the Node object does not already exist is the map.
I suspect (tentatively - I have no way of testing in your code) that you can just use graph.insert( {node, edgeList} );
to insert the relevant pair. Note the curly brackets within the curved brackets. Otherwise you will have to look up a constructor for pair or use make_pair.
At present you are incorrectly trying to add to the map if the node DOES exist in the map - remove the '!' symbol in line 52.
In fact, you probably don't need the add() method at all. graph[node] returns a reference to the mapped value at node (the list of edges), creating it if it doesn't already exist. So, for example, to add an edge to a node, you can simply say:
Thank you all - the curly brackets were all that was needed to successfully insert the pair into the map! Also, I appreciate the attention brought to the faulty if statement logic.