天诺营销怎么样 知乎:c++的作业,那位高手帮忙看看。(函数模版有关)

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/09 02:00:24
c++的作业,那位高手帮忙看看。(函数模版有关)
要求,任意输入三个数,求其最大值,要求有函数模版与函数重载。
本人写的:
#include<iostream.h>
template<class T>
T max(T a,T b,T c)
{
if(a>b)
else
a=b;
if(a>c)
return a;
else
return c;
}
void main()
{ T a,b,c;
cout<<"please input 3 numbers:"<<endl;
cin>>a>>b>>c;
cout<<"the max is:"<<max(a,b,c)<<endl;
}
不知道该怎么弄,调试总是不成功。在这里先谢过了。

有两个错误,if后面如果没有执行语句仍要加“;”;
在主函数中必须指明a,b,c的具体类型。不能是模板;

#include "stdafx.h"

#include<iostream.h>

template<class T>
T max(T a,T b,T c)
{
if(a>b);
else
a=b;
if(a>c)
return a;
else
return c;
}
void main()
{int a,b,c;
cout<<"please input 3 numbers:"<<endl;
cin>>a>>b>>c;
cout<<"the max is:"<<max(a,b,c)<<endl;
}
以上程序执行过了,正确。

————————————————————————
!!!!!!!!!!!!!!!!!!!!!!!!
————————————————————————
#include<iostream.h>
template<class T>
T max(T a,T b,T c)
{ T x;
if(a>b) {x=a;a=b;b=x;}
if(b>c) {x=b;b=c;c=x;}
return c;
}

void main()
{float a,b,c;
cout<<"输入a,b,c的值(数间用空格分开):";
cin>>a>>b>>c;
cout<<"the max is:"<<max(a,b,c)<<endl;
}

什么错误呀??你写的跟例子一样的,看不出有什么错误!