op overloading error

I am not sure why i am getting an error. I dont require it to be a friend, since the settings are public, but assuming if it later became private.

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
#include <iostream>
#include <map>

class GUI{    
    public:
        std::map<std::string, std::string> settings;
        friend std::ostream &operator<<(std::ostream &os, const GUI &obj);
    
        GUI(std::map<std::string, std::string>);
        void show_settings();
};

GUI::GUI(std::map<std::string, std::string> btn_settings){
    this->settings = btn_settings;
}

void GUI::show_settings(){
    std::map<std::string, std::string>::iterator iter;
    for(iter=settings.begin(); iter!=settings.end(); ++iter){
        std::cout << iter->first << " -> " << iter->second << std::endl;
    }
}

std::ostream &operator<<(std::ostream &os, const GUI &obj){
    std::map<std::string, std::string>::iterator iter;
    for(iter=obj.settings.begin(); iter!=obj.settings.end(); ++iter){
        os << iter->first << " -> " << iter->second << std::endl;
    }
    return os;
}

int main(){
    std::map<std::string, std::string> button_settings;
    button_settings["color"]            = "0,0,0";
    button_settings["background_color"] = "255,255,255";
    button_settings["font"]             = "impact.ttf";
    button_settings["fontsize"]         = "15";
    button_settings["border"]           = "False";
    
    GUI button(button_settings);
    //button.show_settings();
    std::cout << button;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
test2.cpp: In function 'std::ostream& operator<<(std::ostream&, const GUI&)':
test2.cpp:27:13: error: no match for 'operator=' (operand types are 'std::map<std::basic_string<char>, std::basic_string<char> >::iterator {aka std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}' and 'std::map<std::basic_string<char>, std::basic_string<char> >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}')
     for(iter=obj.settings.begin(); iter!=obj.settings.end(); ++iter){
             ^
test2.cpp:27:13: note: candidates are:
In file included from /usr/include/c++/4.8.2/map:60:0,
                 from test2.cpp:3:
/usr/include/c++/4.8.2/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)
     struct _Rb_tree_iterator
            ^
/usr/include/c++/4.8.2/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from 'std::map<std::basic_string<char>, std::basic_string<char> >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}' to 'const std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&'
/usr/include/c++/4.8.2/bits/stl_tree.h:157:12: note: std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&)
/usr/include/c++/4.8.2/bits/stl_tree.h:157:12: note:   no known conversion for argument 1 from 'std::map<std::basic_string<char>, std::basic_string<char> >::const_iterator {aka std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}' to 'std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&&'
obj is a const reference so you will have to use a const_iterator.

 
std::map<std::string, std::string>::const_iterator iter;
ah thanks.
Topic archived. No new replies allowed.