#include <iostream>
using namespace std;
template<typename T>
T max(T lhs, T rhs)
{
return lhs < rhs ? rhs : lhs;
}
template<>
int max<int>(int lhs, int rhs)
{
return lhs < rhs ? rhs : lhs;
}
int main()
{
cout << max<int>(4, 5) << endl;
}
~/Documents/C++/boost $ g++ -o testSTL testSTL.cpp -Wall
testSTL.cpp: In function ‘int main()’:
testSTL.cpp:18:24: error: call of overloaded ‘max(int, int)’ is ambiguous
testSTL.cpp:11:5: note: candidates are: T max(T, T) [with T = int]
/usr/include/c++/4.5/bits/stl_algobase.h:209:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
Run Code Online (Sandbox Code Playgroud)
如何更正此错误?
Arm*_*yan 19
这都是因为你的using namespace std;.删除该行.通过using指令,您可以将std::max(必须以某种方式通过iostream包含在内)引入全局范围.因此编译器不知道max调用哪个- ::max或std::max.
我希望这个例子对于那些认为使用指令免费的人来说将是一个很好的稻草人.奇怪的错误是一个副作用.
| 归档时间: |
|
| 查看次数: |
8449 次 |
| 最近记录: |