如果我不重載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