Map Insert Error

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  // DiGraph.h file

#ifndef DIGRAPH_H
#define DIGRAPH_H
#include<string>
#include <map>
#include <vector>
#include <list>
#include <Node.h>
#include <Edge.h>


class DiGraph
{
    public:
        DiGraph();
        ~DiGraph();

        int getSize() const;

        void add(Node node);

    private:
        std::map< Node, std::list<Edge> > graph;
        int graphSize;

};

#endif // DIGRAPH_H



// DiGraph.cpp file begins here:

#include "DiGraph.h"

DiGraph::DiGraph()
{
    graphSize = 0;
}

DiGraph::~DiGraph(){}

int DiGraph::getSize()const{
    return graphSize;
}

void DiGraph::add(Node node){

    std::map<Node, std::list<Edge> >::iterator iter;
    iter = graph.find(node);
    if(!(iter == graph.end())){

        std::list<Edge> edgeList;
        graph.insert(node, edgeList);  // Error message pointing here
    }

}
Have you defined any ordering for class Node? Also, node is not an iterator, so which overload of the insert member function are you trying to use?
Last edited on
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()){

        return true;
    } else {

        return false;
    }
}
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.

I am trying to add to the map if the Node object does not already exist is the map.

But your if statement says if(!(iter == graph.end())){ If should be if (iter == graph.end())

But really there's a much easier way to do this:
1
2
3
void DiGraph::add(Node node){
    graph[node];  // http://www.cplusplus.com/reference/map/map/operator[]/
}


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:
1
2
Edge newEdge = whatever;
graph[node].push_back(newEdge);

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.
Topic archived. No new replies allowed.