C++模板简单问题

JohhGan 2008-07-14 08:20:14
#include<iostream.h>
//using namespace std;
template <typename T>;
T abs<T x>;
{
return x<0? -x:x;
}
int main()
{
int n=-5;
double d=-5.5;
cout<<abs(n)<<endl;
cout<<abs(d)<<endl;
}
怎么错了?
--------------------Configuration: 函数模板 - Win32 Debug--------------------
Compiling...
P285.CPP
D:\c++\pvc6\MyProjects\函数模板\P285.CPP(3) : error C2059: syntax error : '<end Parse>'
D:\c++\pvc6\MyProjects\函数模板\P285.CPP(5) : error C2447: missing function header (old-style formal list?)
D:\c++\pvc6\MyProjects\函数模板\P285.CPP(12) : error C2065: 'abs' : undeclared identifier
D:\c++\pvc6\MyProjects\函数模板\P285.CPP(14) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.

P285.OBJ - 3 error(s), 1 warning(s)
...全文
62 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JohhGan 2008-07-14
  • 打赏
  • 举报
回复
哦!~ 对,我abs后面写成了中括号,应该是小括号!~谢谢哦!~~
Paradin 2008-07-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ttkk_2007 的回复:]
你写错了,注意一点就行了
C/C++ codetemplate<typename T>T abs(T x)
{returnx<0?-x:x;
}intmain()
{intn=-5;doubled=-5.5;
cout<<abs(n)<<endl;
cout<<abs(d)<<endl;
}
[/Quote]
ttkk_2007 2008-07-14
  • 打赏
  • 举报
回复
你写错了,注意一点就行了

template <typename T>
T abs(T x)
{
return x <0? -x:x;
}
int main()
{
int n=-5;
double d=-5.5;
cout <<abs(n) <<endl;
cout <<abs(d) <<endl;
}

64,662

社区成员

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

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