模板功能出错

时间:2012-04-18 09:31:27

标签: c++ templates

此代码有什么问题?

#include <iostream>
using namespace std;

template <typename T>
T max(T X, T Y)
{
    return (X > Y) ? X : Y;
}

int main()
{
    int x = max(5,6);
}

我收到此错误:

overload.C: In function 'int main()':
overload.C:19: error: call of overloaded 'max(int, int)' is ambiguous
overload.C:12: note: candidates are: T max(T, T) [with T = int]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2  /bits/stl_algobase.h:206: note:                 const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]

2 个答案:

答案 0 :(得分:12)

max已在标准库中定义。删除using namespace std,它应该有效。

答案 1 :(得分:0)

标准库可能已经声明了max。尽量不要导入命名空间。