2012-09-24 3 views
0

으로 과 함께 std::map을 사용하려고하는데 이상한 컴파일 오류가 발생합니다. 내가 뭘 잘못하고있어. 다음은 내 코드입니다.std :: map 및 function 포인터 값을

#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

값에 일관된 기능 시그니처가 있어야합니다. –