2013-10-23 46 views
0

爲什麼我收到一個錯誤說:函數模板超載問題

  1. 錯誤C2668:「最大」:不明確調用重載函數
  2. 錯誤C2780:「常量牛逼&最大(常量牛逼&,常量T &,const T &)':期望3個參數 - 2提供。

下面的代碼:

template<typename T> 
inline T const& max(T const& i, T const& j) 
{ 
    cout<<"Using template with 2 args."<<endl; 
    return (i>j) ? i : j; 
} 

template<typename T> 
inline T const& max(T const& i, T const& j, T const& k) 
{ 
    cout<<"Using template with 3 args."<<endl; 
    return max(max(i,j),k); 
} 

void main() 
{ 
    cout<< ::max(1,2,3)<<endl;  
} 

我已經調用它之前定義的參數2模板函數。

回答

2

刪除using namespace std,因爲有std::max,參與搜索,這就是爲什麼你得到第一個錯誤。第二個錯誤簡單地說,有明確的變體,但他們應該得到3個參數。

+0

感謝您的回答。那太傻了。 – Anitesh