Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ map error, with structures and methods, can't convert string to _Tree_iterator<std::_Tree_val<std::_Tree_simple_types< and so on

Bottom line is that I'm writing a method for a structure, that adds to a map that's part of the structure.

In the header file, for the structure, the map variable is declared as map(string,Node) (Node being another structure) (it uses '>'s in the code it's just in this text editor things between those don't show up.)

There is a method for Network that takes an ifstream, and uses getline etc. to take the data in each line and create a node, which is added to the map of the Network variable.

In the header file, this map is called 'nodes'. So in code for the method, I have nodes.insert(string-variable,node-variable). And I get an error going on about trees.

 error C2664: 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::insert(std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,const std::pair<const _Kty,_Ty> &)': cannot convert argument 1 from 'std::string' to 'std::_Tree_const_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>' 

In functions.h, 'Network':

struct Network{
    string label;
    map<string, Node> nodes;
    vector<string> route;

    Network()=default;
    Network(ifstream &);

    string to_string () const;
    Node get_node(string);
    void put_node(Node);
    bool in_route(const Node&);
    Node closest(Node &);
    string calculate_route(const Node&, const Node&);};

'Node' is it's own structure, (int, int, string), coded above Network; let me know if I should include its code too.

Now in functions.cpp, I'm writing the code for the ifstream constructor. I have

Network::Network(ifstream &file) {
    string line;
    Node temp;
    while (getline(file, line)) {
        stuff I know works at creating a node (int, int, string);
        nodes.insert(temp.label, temp);
    }
}

So, I try to add to a map(string,Node) and when I try to use a string Node pair, it says it can't convert from string to something with _Tree s.

I'm in Visual Studio 2015.

I can include more code if need be, I just didn't want to make it any more cluttered than necessary.

like image 370
maejoh Avatar asked Oct 26 '25 07:10

maejoh


1 Answers

Check the documentation for map<K,V>::insert(). All the overloads with two arguments take an iterator as first argument, not a string. This is what your compiler is complaining about, since it's trying and failing to convert the string to an iterator.

You appear to mean

nodes.insert(std::make_pair(temp.label, temp));
like image 91
aschepler Avatar answered Oct 27 '25 20:10

aschepler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!