2012-09-24 32 views
0

我想用std::mapvalue作爲function pointer,但我收到奇怪的編譯錯誤。我在做什麼錯。以下是我的代碼。std ::地圖和函數指針作爲值

#include <map> 
#include <string> 

typedef void (*fptrCmdHandler)(std::string&); 
typedef std::map<std::string, fptrCmdHandler> Cmd2FnMap; 
typedef std::map<std::string, Cmd2FnMap>  Mode2CmdFnMap; 
Mode2CmdFnMap gMode2CmdFnMap; 

class SystemCommands { 
public: 
    static void Help(std::string& result) { 
     result.append("connect").append("\n"); 
     result.append("addserver").append("\n"); 
     result.append("help").append("\n"); 
    } 
}; 
int main(int argc, char **argv) { 
    //Initialize Global Commands 
    gMode2CmdFnMap.insert(std::make_pair("System", std::make_pair("help", &SystemCommands::Help))); 
} 

錯誤是

1>------ Build started: Project: test, Configuration: Debug Win32 ------ 
1> test.cpp 
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(163): error C2664: 'std::map<_Kty,_Ty>::map(const std::map<_Kty,_Ty> &)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'const std::map<_Kty,_Ty> &' 
1>   with 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=fptrCmdHandler 
1>   ] 
1>   and 
1>   [ 
1>    _Ty1=const char *, 
1>    _Ty2=void (__cdecl *)(std::string &) 
1>   ] 
1>   and 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=fptrCmdHandler 
1>   ] 
1>   Reason: cannot convert from 'std::pair<_Ty1,_Ty2>' to 'const std::map<_Kty,_Ty>' 
1>   with 
1>   [ 
1>    _Ty1=const char *, 
1>    _Ty2=void (__cdecl *)(std::string &) 
1>   ] 
1>   and 
1>   [ 
1>    _Kty=std::string, 
1>    _Ty=fptrCmdHandler 
1>   ] 
1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(255) : see reference to function template instantiation 'std::_Pair_base<_Ty1,_Ty2>::_Pair_base<_Ty,std::pair<const char *,void (__cdecl *)(std::string &)>>(_Other1 &&,_Other2 &&)' being compiled 
1>   with 
1>   [ 
1>    _Ty1=const std::string, 
1>    _Ty2=Cmd2FnMap, 
1>    _Ty=const char *, 
1>    _Other1=const char *, 
1>    _Other2=std::pair<const char *,void (__cdecl *)(std::string &)> 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory(208) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair<const char*,std::pair<const char *,void (__cdecl *)(std::string &)>>(std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>> &&)' being compiled 
1>   with 
1>   [ 
1>    _Ty1=const std::string, 
1>    _Ty2=Cmd2FnMap 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xmemory(280) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<std::pair<_Ty1,_Ty2>>(std::pair<const _Kty,std::map<_Kty,fptrCmdHandler>> ,_Other &&)' being compiled 
1>   with 
1>   [ 
1>    _Ty=std::pair<const std::string,Cmd2FnMap>, 
1>    _Ty1=const char *, 
1>    _Ty2=std::pair<const char *,void (__cdecl *)(std::string &)>, 
1>    _Kty=std::string, 
1>    _Other=std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>> 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtree(592) : see reference to function template instantiation 'void std::_Cons_val<std::allocator<_Ty>,_Ty,std::pair<_Ty1,_Ty2>>(_Alloc &,std::pair<const _Kty,std::map<_Kty,fptrCmdHandler>> *,std::pair<_Ty1,_Ty2> &&)' being compiled 
1>   with 
1>   [ 
1>    _Ty=std::pair<const std::string,Cmd2FnMap>, 
1>    _Ty1=const char *, 
1>    _Ty2=std::pair<const char *,void (__cdecl *)(std::string &)>, 
1>    _Alloc=std::allocator<std::pair<const std::string,Cmd2FnMap>>, 
1>    _Kty=std::string 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtree(755) : see reference to function template instantiation 'std::_Tree_nod<_Traits>::_Node *std::_Tree_val<_Traits>::_Buynode<_Ty>(_Valty &&)' being compiled 
1>   with 
1>   [ 
1>    _Traits=std::_Tmap_traits<std::string,Cmd2FnMap,std::less<std::string>,std::allocator<std::pair<const std::string,Cmd2FnMap>>,false>, 
1>    _Ty=std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>>, 
1>    _Valty=std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>> 
1>   ] 
1>   e:\avinash\test\test\test.cpp(19) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>>>(_Valty &&)' being compiled 
1>   with 
1>   [ 
1>    _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tmap_traits<std::string,Cmd2FnMap,std::less<std::string>,std::allocator<std::pair<const std::string,Cmd2FnMap>>,false>>>, 
1>    _Ty2=bool, 
1>    _Traits=std::_Tmap_traits<std::string,Cmd2FnMap,std::less<std::string>,std::allocator<std::pair<const std::string,Cmd2FnMap>>,false>, 
1>    _Valty=std::pair<const char *,std::pair<const char *,void (__cdecl *)(std::string &)>> 
1>   ] 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

回答

5

您試圖插入一對在那裏想要的地圖。 這對插入很好,但應該是pair<string, map<string, F>>

爲什麼不直接說:

gMode2CmdFnMap["System"]["help"] = &SystemCommands::Help; 

您不必擔心效率過於這裏 - 有多少次將你的程序的運行過程中該代碼執行?

+0

請注意,您的值中必須包含一致的函數簽名。 –