Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板函数中存在错误_C++_Templates - Fatal编程技术网

C++ 模板函数中存在错误

C++ 模板函数中存在错误,c++,templates,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); } max已在标准库中定义。使用namespace std删除,它应该可以工作。可能max已经由standrad库声明。尝试不导入名称空间 +1这是一个

这个代码有什么问题

#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);
}

max
已在标准库中定义。使用namespace std删除
,它应该可以工作。

可能
max
已经由standrad库声明。尝试不导入名称空间

+1这是一个链接,仅供参考:+1:但可能需要添加一条注释,说明标准并不禁止标准头包含其他标准头,这会导致
悄悄进入。或者,您可以使用完全限定名
::max
引用全局命名空间中的声明。但是使用
删除
会更礼貌;使用std::max而不是编写自己的版本可能更好。据我所知,构建一个好的编译器错误检测和报告系统是一项非常复杂的任务!模板让它更难!
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]