【发布时间】:2010-04-30 16:08:42
【问题描述】:
如果我不重载 myfunc,则可以工作的简单代码。
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)”:无法从“std::_Vector_iterator<_ty>”推导出“_InIt”的模板参数。
如果我不重载 myfunc,它确实有效。有人可以解释这里发生了什么。
TIA
【问题讨论】:
-
看起来你的代码标签倒退了。你能解决吗?
-
编译器告诉你参数不明确。您是否希望编译器为您选择其中之一?好问题是“我怎样才能消除这个电话的歧义?”。