首页 文章

如何从C中的map迭代器调用类成员函数?

提问于
浏览
-1

我无法使用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/元组:在实例化'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> :: 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:从'std:需要: :map <_Key,_Tp,_Compare,_Alloc> :: mapped_type&std :: map <_Key,_Tp,_Compare,_Alloc> :: operator [](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()'秒(std :: forward <Args2>(std :: get <Indexes2>( tuple2))...)^ / usr / include / c /4.9/tuple:1104:70:注意:候选人是:9:5:注意:A :: A(int)9:5:注意:候选人需要1个参数,0提供5:7:注意:constexpr A :: A(const A&)5:7:注意:候选人期望1个参数,0提供

4 回答

  • 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;
    }
    
  • 0

    首先,你需要A中的默认构造函数:

    A() {}
    

    如果您提供了任何其他c-tor,编译器将不会创建默认构造函数 .

    第二件事是如何调用你的函数:

    it->second.show();
    
  • 0

    这将解决你的麻烦

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

    你应该覆盖operator =以使其正常工作

  • 0
    1. it->second 将直接返回 A ,而不是 A* ,您应该更改
    (*(it->second)).show();
    

    (it->second).show();
    
    1. std::map::operator[]需要输入 ADefaultConstructible .

    如果密钥不存在,则插入value_type(key,T()) .

    A 没有默认构造函数,你可以

    mymap.insert({'a', a});
    

    或者@ Jarod42建议:

    mymap.emplace('a', a);
    

    避免 A 被默认构造 .

相关问题