我正在尝试将std::map
与value
一起用作function pointer
,但我得到了奇怪的编译错误。我在做什么是错的。以下是我的代码。
#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 ==========
答案 0 :(得分:5)
您正试图在想要地图的地方插入一对。
对插入很好,但它应该是pair<string, map<string, F>>
。
为什么不说:
gMode2CmdFnMap["System"]["help"] = &SystemCommands::Help;
这里你不需要太担心效率 - 在程序运行期间这段代码会执行多少次?