这两中形式~~~帮忙看看~~弄的好郁闷~~在线ing

block1956815 2004-09-09 10:15:12
写个简单的测试 看看这两中情况有什么不同:
//in test.h

#ifdef sample
// sample 1
template <class T>
inline T max(T t1 , T t2)
{
return (t1 > t2)? t1:t2;
}
#else
//sample 2
template <class T>
inline T max<class T>(T t1 , T t2)
{
return (t1 > t2)? t1:t2;
}
#endif
哪中形式是标准形式 怎么两种情况都通不过编译? 两种情况有什么不同? 多谢
...全文
153 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
block1956815 2004-09-09
  • 打赏
  • 举报
回复
多谢大家~~~
时间关系 我试试 明天揭贴~~~
snow810211 2004-09-09
  • 打赏
  • 举报
回复
好像是这样的,定义的时候,应该写成你的第一种格式,然后调用模版函数的时候,也就是函数模版的实例化过程,格式是:
函数名<模版实参表>(模版函数实参表)
其中,“<class T>”叫做模版实参表,他被省略的条件是:编译系统可以从模版函数的实参表中获得该信息。
如:
template <class T>
T max(T x, T y)
{
return (x>y)?x:y;
}

调用的时候,如果是:max(3,5)
那么其中的“max<int>(3,5)”中的<int>可以省略,因为无论从3还是5都能知道是int,但是如果调用的时候是max(3,5.0),则就不能省略了,必须写成“max<int>(3,5.0)/max<double(3,5.0)>”.

这是我的理解,请参考~~
BluntBlade 2004-09-09
  • 打赏
  • 举报
回复
VC 7.1下通过。

#include "stdafx.h"
#include <iosfwd>

using namespace std;

template <typename T>
inline T MaxNum(T t1, T t2)
{
return (t1 > t2) ? t1 : t2;
}

int _tmain(int argc, _TCHAR* argv[])
{
int a = 5, b = 4;

cout << MaxNum(a,b) << endl;

return 0;
}
ftkghost 2004-09-09
  • 打赏
  • 举报
回复
o 不好意思 没看到:(
最近打CS眼睛走火
Leaf_jo 2004-09-09
  • 打赏
  • 举报
回复
inline T max<class T>(T t1 , T t2)这个地方不对啊!
改成inline T max(T t1 , T t2)
BluntBlade 2004-09-09
  • 打赏
  • 举报
回复
你最好把max放入一个名空间,或者换个名字。
因为有std::max这么一个东西。
block1956815 2004-09-09
  • 打赏
  • 举报
回复
测试代码如下:
main()
{
int t1 = 2;
int t2 = 3;
int rt=max<int>(t1 , t2);
cout<<"rt="<<rt<<endl;
}

第一中情况下错误提示:

f:\vpn\testprojects\stl\stl.h(18) : error C2059: syntax error : 'function-style cast'
f:\vpn\testprojects\stl\stl.h(18) : error C2226: syntax error : unexpected type 'T'
f:\vpn\testprojects\stl\stl.h(18) : error C2059: syntax error : ')'
F:\VPN\TestProjects\Stl\Stl.cpp(42) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
------------------------------------------------------------------------

第二种情况下错误提示:

:\vpn\testprojects\stl\stl.h(28) : error C2143: syntax error : missing ';' before '<'
f:\vpn\testprojects\stl\stl.h(28) : error C2433: 'max' : 'inline' not permitted on data declarations
f:\vpn\testprojects\stl\stl.h(28) : error C2059: syntax error : ';'
f:\vpn\testprojects\stl\stl.h(28) : error C2143: syntax error : missing ';' before '<'
F:\VPN\TestProjects\Stl\Stl.cpp(42) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
block1956815 2004-09-09
  • 打赏
  • 举报
回复
to: ftkghost(小鹏)
拜托~~~ 请看清楚的说~~~
ftkghost 2004-09-09
  • 打赏
  • 举报
回复
#ifdef与#endif要成对使用
ftkghost 2004-09-09
  • 打赏
  • 举报
回复
楼主差了一句#endif
BluntBlade 2004-09-09
  • 打赏
  • 举报
回复
语法上倒没什么问题。
不过返回值类型无法被推断。你可以这样调用第一个示例:
max<int>(1,2);

64,654

社区成员

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

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