我正在练习使用函数模板
按教科书写的程序一如下
#include<iostream>
using namespace std;
template<typename T>;
T max(T m,T n)
{
if(m>=n)
return m;
else
return n;
}
int main()
{
int a,b;
float c,d;
cin>>a>>b>>c>>d;
cout<<"max="<<max(a,b)<<endl<<"max="<<max(c,d)<<endl;
return 0;
}
竟然错误了
错误信息如下G:\c++练习\3.cpp(3) : error C2059: syntax error : '<end Parse>'
G:\c++练习\3.cpp(5) : error C2143: syntax error : missing ';' before '{'
G:\c++练习\3.cpp(5) : error C2447: missing function header (old-style formal list?)
G:\c++练习\3.cpp(15) : error C2065: 'a' : undeclared identifier
G:\c++练习\3.cpp(15) : error C2065: 'b' : undeclared identifier
G:\c++练习\3.cpp(15) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
我在网上查到另一种定义方法 运行正确
#include<iostream>
using namespace std;
template<typename T>
T max(T m,T n)//!
{
if(m>=n)
return m;
else
return n;
}
int main()
{
int a,b;
float c,d;
cin>>a>>b>>c>>d;
cout<<"max="<<max(a,b)<<endl<<"max="<<max(c,d)<<endl;
return 0;
}
这是怎么回事?? 难道教材错了??? 两种定义方法属于不同编译系统???
请教高手 谢谢