我刚刚开始使用 c++ 中的函数模板。我正在使用这些教程。我正在尝试实现一个类似这样的基本代码。
#include<iostream>
using namespace std;
template<class t>
t max(t a,t b){
t max_value;
max_value = (a>b)?a:b;
return max_value;
}
int main(){
int a=9,b=8,c;
c=max<int>(a,b);
cout<<c;
return 0;
}
但是我收到以下错误。
/usr/bin/gmake" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
gmake[1]: Entering directory `/home/gursheel/NetBeansProjects/project_q7'
"/usr/bin/gmake" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/project_q7
gmake[2]: Entering directory `/home/gursheel/NetBeansProjects/project_q7'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:16:19: error: call of overloaded ‘max(int&, int&)’ is ambiguous
main.cpp:16:19: note: candidates are:
main.cpp:4:3: note: t max(t, t) [with t = int]
In file included from /usr/include/c++/4.7/bits/char_traits.h:41:0,
from /usr/include/c++/4.7/ios:41,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from main.cpp:1:
/usr/include/c++/4.7/bits/stl_algobase.h:210:5: note: const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]
gmake[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
gmake[2]: Leaving directory `/home/gursheel/NetBeansProjects/project_q7'
gmake[1]: *** [.build-conf] Error 2
gmake[1]: Leaving directory `/home/gursheel/NetBeansProjects/project_q7'
gmake: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 318ms)
我无法理解错误到底是什么。任何帮助,将不胜感激。