How to call class member function from iterator of map in C++?

I am not able to call the show function using iterator of map. Is there any way to do this using iterator?

#include <iostream>
#include <string>
#include <map>
using namespace std;
class A 
{ 
    int i;
    public: 
    A(int pi):i(pi)   {  cout<<"A()n"; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()n"; }
};
int main()
{
    map<char, A > mymap;
    A a(9) , b(8) , c(7);
    mymap['a'] =  a;
    mymap['b'] =  b;
    mymap['c'] =  c;
    map<char,A >::iterator it;
    for(it = mymap.begin(); it != mymap.end() ; it++)
        (*(it->second)).show();
    return 0;
}

On using it->second.show() , I get the error below:

In file included from /usr/include/c++/4.9/bits/stl_map.h:63:0, from /usr/include/c++/4.9/map:61, from 3: /usr/include/c++/4.9/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {char&&}; long unsigned int ..._Indexes1 = {0ul}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const char; _T2 = A]': /usr/include/c++/4.9/tuple:1093:63: required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {char&&}; _Args2 = {}; _T1 = const char; _T2 = A]' /usr/include/c++/4.9/ext/new_allocator.h:120:4: required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair; _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Tp = std::_Rb_tree_node >]' /usr/include/c++/4.9/bits/alloc_traits.h:253:4: required from 'static std::_Require::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair; _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Alloc = std::allocator > >; std::_Require::__construct_helper<_Tp, _Args>::type> = void]' /usr/include/c++/4.9/bits/alloc_traits.h:399:57: required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair; _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Alloc = std::allocator > >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = ]' /usr/include/c++/4.9/bits/stl_tree.h:423:42: required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Key = char; _Val = std::pair; _KeyOfValue = std::_Select1st >; _Compare = std::less; _Alloc = std::allocator >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node >*]' /usr/include/c++/4.9/bits/stl_tree.h:1790:64: required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple, std::tuple<>}; _Key = char; _Val = std::pair; _KeyOfValue = std::_Select1st >; _Compare = std::less; _Alloc = std::allocator >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >]' /usr/include/c++/4.9/bits/stl_map.h:519:8: required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = char; _Tp = A; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = A; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]' 17:14: required from here /usr/include/c++/4.9/tuple:1104:70: error: no matching function for call to 'A::A()' second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...) ^ /usr/include/c++/4.9/tuple:1104:70: note: candidates are: 9:5: note: A::A(int) 9:5: note: candidate expects 1 argument, 0 provided 5:7: note: constexpr A::A(const A&) 5:7: note: candidate expects 1 argument, 0 provided


how about this? friend.

#include <iostream>
#include <string>
#include <map>
using namespace std;
class A 
{ 
    int i;
public: 
    A(int pi=0):i(pi)   {  cout<<"A()n"; }
    void show() const { cout<<i<<endl; }
    ~A()   {  cout<<"~A()n"; }
};

int main()
{
    map<char, A > mymap;
    A a(9) , b(8) , c(7);
    mymap['a'] =  a;
    mymap['b'] =  b;
    mymap['c'] =  c;
    map<char,A >::iterator it;
    for(it = mymap.begin(); it != mymap.end() ; it++)
        it->second.show();
    return 0;
}

First thing is that you need a default constructor in A:

A() {}

compiler will not create default constructor if you have provided any other c-tor.

second thing is how to call your function:

it->second.show();

this will solve your troubles

    A(int pi=0):i(pi)   {  cout<<"A("<<pi<<")n"; }
...
    for(it = mymap.begin(); it != mymap.end() ; it++)
        (it->second).show();

u should override operator = to make it work properly

链接地址: http://www.djcxy.com/p/72540.html

上一篇: 请解释Kernighan位计算算法的逻辑

下一篇: 如何从C ++的迭代器中调用类成员函数?