如果我不重载myfunc,它可以工作。C++ STL 101:过载函数会导致编译错误
void myfunc(int i)
{
std::cout << "calling myfunc with arg " << i << std::endl;
}
void myfunc(std::string s)
{
std::cout << "calling myfunc with arg " << s << std::endl;
}
void testalgos()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
std::vector<std::string> s;
s.push_back("one");
s.push_back("two");
std::for_each(v.begin(), v.end(), myfunc);
std::for_each(s.begin(), s.end(), myfunc);
return;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello World" << std::endl;
testalgos();
return 0;
}
以下构建错误会针对for_each调用重复进行。
错误C2914: '的std :: for_each的':不能推导出模板参数的函数的参数不明确 错误C2784: '_Fn1的std :: for_each的(_Init,_init,_Fn1)':不能推导出模板参数'_InIt'from'std :: _ Vector_iterator < _Ty,_Alloc>'。
如果我不重载myfunc,它可以工作。有人解释这里发生了什么。
TIA
看起来像你有你的代码标签倒退。你能解决吗? – 2010-04-30 16:09:43
编译器告诉你这个说法是不明确的。你是否期望编译器为你选择其中之一?好的问题是“我怎样才能消除这个电话的歧义?”。 – 2010-04-30 17:29:33