错误:重载的“ max(int,int)”的调用不明确 [英] error: call of overloaded ‘max(int, int)’ is ambiguous
本文介绍了错误:重载的“ max(int,int)”的调用不明确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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]
怎么办我纠正了这个错误吗?
How do I correct this error?
推荐答案
这都是因为您使用命名空间std;
。删除该行。
通过该使用指令,您将 std :: max
(必须通过iostream包括在内)带入全局范围。因此,编译器不知道要调用哪个 max
- :: max
或 std: :max
。
It's all because of your using namespace std;
. Remove that line.
By that using-directive, you bring std::max
(which must be somehow included via iostream) into the global scope. Therefore the compiler doesn't know which max
to call - ::max
or std::max
.
我希望这个例子对那些认为使用指令是免费的人来说是一个很好的稻草人。奇怪的错误是一种副作用。
I hope this example will be a good scarecrow for those who think that using directives come at no cost. Weird errors are one side effect.
这篇关于错误:重载的“ max(int,int)”的调用不明确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文