各位请帮我纠正这程序,THANK YOU!!

andyone11 2003-07-15 05:07:10
程序题目如下:
编写一个函数模板,它返回两个值中的最小者,但同时要确保能正确处理字符串.
我编的程序如下:
#include<iostream.h>
#include<string.h>
template<class T>
T Min(T a,T b)
{
if(sizeof(T)<2)
return(strcmp(a,b)<0?a:b);
/*'strcmp' : cannot convert parameter 1 from 'const int' to 'const char *'*/

return a<b?a:b;
}
void main()
{
cout<<"Min(3,5) is"
<<Min(3,5)<<endl;
/*see reference to function template instantiation 'const int __cdecl Min(const int &,const int &)' being compiled*/
cout<<"Min('3','5')is"
<<Min('3','5')<<endl;
}
...全文
72 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hypp 2003-07-16
  • 打赏
  • 举报
回复
T Min(T a,T b)
{
if(sizeof(T)<2)
return(strcmp(a,b)<0?a:b);
/*'strcmp' : cannot convert parameter 1 from 'const int' to 'const char *'*/

return a<b?a:b;
}'
strcmp()的原形声明为:
int strcmp(const char* str1,const char* str2);
//Both str1 and str2 are constant character strings which cann't be change in the //body of the function.
他们是比较字符串与基本类型不同
所以mint()可以比较几种基本类型(int,float ,double, char)但是字符串是不行的,正因为如此才有strcmp()这个函数.
min(int mark,T&,T&)//mark is label .if mark=1 is not the basical type of //data,mark=1,else mark=0
分开处理不同类型.
int mint(int mark,T& t1,T& t2)
{if(mark)
return strcmp(t1,t2);
else
return t1<t2;
}
andyone11 2003-07-16
  • 打赏
  • 举报
回复
我想这道题应该用两个函数才行,因为strcmp函数只能用于比较字符串,若出现double型的不就不能比较啦!!!
哪位高手能一个函数搞定它.
偶配服他,
andyone11 2003-07-16
  • 打赏
  • 举报
回复
只有sizeof(char)<2.其它的都>=2
RedSunRS 2003-07-16
  • 打赏
  • 举报
回复
sizeof(T)<2?倒是哪种类型的大小小于2呢?从是std::string?不会吧,它的大小是16?
各位高手请赐教。
RedSunRS 2003-07-16
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
template<class T>
T Min(const T *a,const T * b)
{
if(sizeof(T)<2)
return(strcmp(a,b)<0?a:b);

return a<b?a:b;
}
void main()
{
// const int a=3,b=5;
cout<<"Min(&3,&5) is"
<<Min(3,5)<<endl;
//你的声明是指针(constT*),而你给的值无法从const int-->const int*

cout<<Min("hello","go")
<<endl;
}
这样的话就只有一个错.不过我不知道为什么错,请指教,必给分,THANK YOU

andyone11 2003-07-15
  • 打赏
  • 举报
回复
还是有这个错误啊,倒数第二个语句
'T __cdecl Min(const T *,const T *)' : could not deduce template argument for 'const *' from 'int'
MaiCle 2003-07-15
  • 打赏
  • 举报
回复
void main()
{
// const int a=3,b=5;
int a = 3, b = 5;
char c1[10] = "hello";
char c2[10] = "go";
cout<<"Min(&3,&5) is"<<Min(a,b)<<endl;
cout<<Min(c1,c2)<<endl;
}
andyone11 2003-07-15
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
template<class T>
T Min(const T *a,const T * b)
{
if(sizeof(T)<2)
return(strcmp(a,b)<0?a:b);

return a<b?a:b;
}
void main()
{
// const int a=3,b=5;
cout<<"Min(&3,&5) is"
<<Min(3,5)<<endl;
cout<<Min("hello","go")
<<endl;
}
这样的话就只有一个错.不过我不知道为什么错,请指教,必给分,THANK YOU
andyone11 2003-07-15
  • 打赏
  • 举报
回复
这样才算是考虑到字符串
andyone11 2003-07-15
  • 打赏
  • 举报
回复
sorry,应把源程序main函数改一下
cout<<"Min('3','5')is"
<<Min('3','5')<<endl;
改为:cout<<"Min("hello","god")"<<Min("hello","god")<<endl;
bigtea 2003-07-15
  • 打赏
  • 举报
回复
因为char 和 int 在计算机内部实际是一回事。
bigtea 2003-07-15
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
template<class T>
T Min(T a,T b)
{
//if(sizeof(T)<2)
//return(strcmp(a,b)<0?a:b); 不必要
/*'strcmp' : cannot convert parameter 1 from 'const int' to 'const char *'*/

return a<b?a:b;
}
void main()
{
cout<<"Min(3,5) is"
<<Min(3,5)<<endl;
/*see reference to function template instantiation 'const int __cdecl Min(const int &,const int &)' being compiled*/
cout<<"Min('3','5')is"
<<Min('3','5')<<endl;
}
输出
Min(3,5) is3
Min('3','5')is3
Press any key to continue

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧