模板类问题

xuxian02092213 2009-03-10 12:52:47
我在写程序的时候,使用到了模板类,但是不会用,出现了错误,还请过来的高手指点一二
template<class T,int MAX_SIZE>
class STemplateObjMan
{
public:
DWORD FreeHead;
SObj* GetNew();
BOOL FreeObj(SObj*);
T* operator ++(int ){return (T*)GetNew();}
T* operator ++() {return (T*)GetNew();}
//...
}

//...
template <class T,int MAX_SIZE>
SObj* STemplateObjMan<T,MAX_SIZE>::GetNew()
{
if(FreeHead == -1) //没有空余的资源供使用,只能返回空值
return NULL;
//..
}


出现了错误,错误是
d:\mytest\netpart\sobj.h(91) : error C2143: syntax error : missing ')' before ';'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(91) : error C2059: syntax error : ')'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(92) : warning C4390: ';' : empty controlled statement found; is this the intent?
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(98) : error C2143: syntax error : missing ')' before ';'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(98) : error C2059: syntax error : ')'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(99) : warning C4390: ';' : empty controlled statement found; is this the intent?
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class CANDATA,10000>::GetNew(void)'
d:\mytest\netpart\sobj.h(91) : error C2143: syntax error : missing ')' before ';'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
d:\mytest\netpart\sobj.h(91) : error C2059: syntax error : ')'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
d:\mytest\netpart\sobj.h(92) : warning C4390: ';' : empty controlled statement found; is this the intent?
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
d:\mytest\netpart\sobj.h(98) : error C2143: syntax error : missing ')' before ';'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
d:\mytest\netpart\sobj.h(98) : error C2059: syntax error : ')'
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
d:\mytest\netpart\sobj.h(99) : warning C4390: ';' : empty controlled statement found; is this the intent?
d:\mytest\netpart\sobj.h(52) : while compiling class-template member function 'class SObj *__thiscall STemplateObjMan<class SIOData,100>::GetNew(void)'
SObj.cpp

还请那位高手指教
...全文
258 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
downmooner 2009-03-23
  • 打赏
  • 举报
回复
学习了.
maying_11 2009-03-23
  • 打赏
  • 举报
回复
好象编译器不支持模板分离模式.声明和定义应该都在一个文件里吧!
leo315 2009-03-23
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 cswat 的回复:]
呵呵, 本来是想来学点东西的, 现在弄的稀里糊涂的,让看的人都不知道问题在那,呵呵,就楼主清楚了,给分吧
[/Quote]

果然是个没有营养的帖子
cswat 2009-03-22
  • 打赏
  • 举报
回复
呵呵, 本来是想来学点东西的, 现在弄的稀里糊涂的,让看的人都不知道问题在那,呵呵,就楼主清楚了,给分吧
xuxian02092213 2009-03-22
  • 打赏
  • 举报
回复
我知道问题出在哪了,多些楼上的各位大力支持
chin_chen 2009-03-10
  • 打赏
  • 举报
回复
怎么全是头文件的错误。
sagegz 2009-03-10
  • 打赏
  • 举报
回复
的确少了个分号...
pengzhixi 2009-03-10
  • 打赏
  • 举报
回复
我估计是你的模板的声明和定义不在同一个文件.
lingyin55 2009-03-10
  • 打赏
  • 举报
回复
好好检查一下声明类的头文件看看,可能是那里缺少符号。
ypb362148418 2009-03-10
  • 打赏
  • 举报
回复
看到这么多错误都眼花了,就这些似乎没法看出来错误啊,将你的源代码全部贴出来吧,好让大家看看
KevinYuen 2009-03-10
  • 打赏
  • 举报
回复

class SObj;
template<class T,int MAX_SIZE>
class STemplateObjMan
{
public:
DWORD FreeHead;
SObj* GetNew();
BOOL FreeObj(SObj*);
T* operator ++(int ){return (T*)GetNew();}
T* operator ++() {return (T*)GetNew();}
//...
};

//...
template <class T,int MAX_SIZE>
SObj* STemplateObjMan<T,MAX_SIZE>::GetNew()
{
if(FreeHead == -1) //没有空余的资源供使用,只能返回空值
return NULL;
//..
}


加上DWORD的windows.h头文件和Class Sobj的前项申明,就可以编译过去了,如果楼主还是编译不过去那就是该文件包含的某个文件的问题了,可能是sobj.h这个文件里的问题,可以把代码贴上来看看
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuxian02092213 的回复:]
上面给出的少了一个分号,但程序中并没有少,所以不是这个问题
[/Quote]
那就不清楚了,我拿这个代码copy 然后编译,就没问题.
chin_chen 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuxian02092213 的回复:]
上面给出的少了一个分号,但程序中并没有少,所以不是这个问题
[/Quote]
我就知道你省略了很多东西。不如都贴出来撒。光着几行看不出来的。错误怎么都在同文件中。
d:\mytest\netpart\sobj.h(98) : error C2059: syntax error : ')'
while compiling class-template member function 'class SObj *__thiscall STemplateObjMan <class CANDATA,10000>::GetNew(void)
估计你其它地方引用这个函数,少了什么,或者这个函数写的时候少了什么
  • 打赏
  • 举报
回复
先问个问题..模板类的声明跟定义都在一个文件里吧?
  • 打赏
  • 举报
回复
class STemplateObjMan
{
public:
DWORD FreeHead;
SObj* GetNew();
BOOL FreeObj(SObj*);
T* operator ++(int ){return (T*)GetNew();}
T* operator ++() {return (T*)GetNew();}
//...
}

//汗,又是;符号
xuxian02092213 2009-03-10
  • 打赏
  • 举报
回复
上面给出的少了一个分号,但程序中并没有少,所以不是这个问题
  • 打赏
  • 举报
回复

template<class T,int MAX_SIZE>
class STemplateObjMan
{
public:
DWORD FreeHead;
SObj* GetNew();
BOOL FreeObj(SObj*);
T* operator ++(int ){return (T*)GetNew();}
T* operator ++() {return (T*)GetNew();}
//...
};//<=================少了一个;号

65,210

社区成员

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

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