爲什麼我會收到編譯錯誤no matching function for call to `f(__gnu_cxx::__normal_iterator > >)'
?爲什麼C++不能找到模板函數?
#include <vector>
template<typename T>
void f(const typename std::vector<T>::iterator &) {}
void g() {
std::vector<int> v;
f<int>(v.end()); // Compiles.
f(v.end()); // Doesn't compile, gcc 4.3 can't find any match.
}
最後,我想編寫一個函數,只需要一個矢量迭代器和編譯失敗(有一個有意義的錯誤)的任何東西。所以template<typename T>void f(const T&) {}
不是一個好的解決方案,因爲它也編譯爲其他類型。
這有什麼錯你有?編譯錯誤的原因是您沒有爲模板化函數調用提供類型。即使你提供了'int'作爲向量類型,你仍然需要爲'f'調用指定它。 – Chris
[C++,模板參數不能被推斷]的可能的重複(http://stackoverflow.com/questions/6060824/c-template-argument-can-not-be-deduced) – Mehrdad