我正在尝试使用以下代码将函数指针作为值存储在映射中。我正在开发 solaris sparc 服务器。下面是我写的代码。
#include<iostream>
#include<sstream>
#include<map>
using namespace std;
typedef int (*spl_handler)(int,string,char *);
typedef map<string, spl_handler> mo_map;
int foo(int,char,char *);
int bar(int,char,char *);
int foo(int a,string b,char * str)
{
cout <<"i am in foo "<<a<<endl;
cout <<"i am in foo "<<b<<endl;
cout <<"i am in foo "<<str<<endl;
return 0;
}
int bar(int a,string b,char * str)
{
cout <<"i am in bar "<<a<<endl;
cout <<"i am in bar "<<b<<endl;
cout <<"i am in bar "<<str<<endl;
return 0;
}
int main()
{
mo_map m;
//m["foo"]=&foo; //compiling works if i write like this .
m.insert(std::pair<string,spl_handler>(string("foo"),&foo)); //compilation fails here????
return 0;
}
我收到一条错误消息:
> CC spl_handler.cc
"spl_handler.cc", line 34: Error: Could not find a match for std::pair<std::string, int(*)(int,std::string,char*)>::pair(std::string, int(*)(int,char,char*)) needed in main().
1 Error(s) detected.
但是没有插入就直接分配有效。
谁能告诉我为什么编译在这里失败?