如何从C ++的迭代器中调用类成员函数?
我无法使用map的迭代器调用show函数。 有没有办法使用迭代器来做到这一点?
#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;
}
在使用it->second.show()
,我得到下面的错误:
在/usr/include/c++/4.9/bits/stl_map.h:63:0包含的文件中,从/usr/include/c++/4.9/map:61开始,从3:/usr/include/c++/4.9/tuple :在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:需要从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:需要从'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:需要来自'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:'static decltype(_S_construct(__ a,__p ,(forward <_Args>)(std :: allocator_traits :: construct :: __ args)...))std :: allocator_traits <_Alloc> :: construct(_Alloc&,_Tp *,_Args && ...)[with _Tp = std ::对; _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:需要从'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:'std :: _ Rb_tree <_Key,_Val,_KeyOfValue,_Compare,_Alloc> ::迭代器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:'std: :map <_Key,_Tp,_Compare,_Alloc> :: mapped_type&std :: map <_Key,_Tp,_Compare,_Alloc> ::运算符[](std :: map <_Key,_Tp,_Compare,_Alloc> :: key_type &&) [用_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:必需从这里/usr/include/c++/4.9/tuple:1104:70:错误:没有匹配函数调用'A :: A()'second(std :: forward <_Args2>(std :: get <_Indexes2>(__ tuple2))...)^ /usr/include/c++/4.9/tuple:1104:70:note :候选人是:9:5:注意:A :: A(int)9:5:注意:候选人期望1个参数,0提供5:7:注意:constexpr A :: A(常量A&)5:7: :候选人期望1个参数,0提供
这个怎么样? 朋友。
#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;
}
首先,您需要A中的默认构造函数:
A() {}
如果你已经提供了其他的c-tor,编译器不会创建默认的构造函数。
第二件事是如何调用你的函数:
it->second.show();
这将解决你的麻烦
A(int pi=0):i(pi) { cout<<"A("<<pi<<")n"; }
...
for(it = mymap.begin(); it != mymap.end() ; it++)
(it->second).show();
你应该重载operator =以使其正常工作
链接地址: http://www.djcxy.com/p/72539.html上一篇: How to call class member function from iterator of map in C++?
下一篇: Is there a better implementation for keeping a count for unique integer pairs?