大众信息网

一个visual c++ 6.0 函数模板问题

关注:215  答案:2  手机版
解决时间 2021-08-14 03:20

我正在练习使用函数模板

按教科书写的程序一如下

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

这是怎么回事?? 难道教材错了??? 两种定义方法属于不同编译系统???

请教高手  谢谢

 

最佳答案
template<typename T>;后面的分别去掉就可以了
全部回答

第一个里面:

template<typename T>;

为什么后面有逗号,还是中文的逗号!!!

我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!