C++题解为什么通不过编译
C++例题为什么通不过编译?#includeiostreamusing namespace stdtemplateclass TT max(T ml,T m2){ret
C++例题为什么通不过编译?
#include<iostream>
using namespace std;
template<class T>
T max(T ml,T m2)
{return (m1>m2)?ml:m2;}
void main( ) {
cout<<max(2,5)<< "\t"<<max(2.0,5.0)<<endl;
}
编译错误提示:
D:\Program Files\Microsoft Visual Studio\MyProjects\001\1.cpp(5) : error C2065: 'm1' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\001\1.cpp(7) : see reference to function template instantiation 'int __cdecl max(int,int)' being compiled
Error executing cl.exe.
是书上的例题,哪错了?
[解决办法]#include<iostream>
using namespace std;
template<class T>
T Max(T m1,T m2)
{return (m1>m2)?m1:m2;}
void main( ) {
cout<<Max(2,5)<< "\t"<<Max(2.0,5.0)<<endl;
}