难道又是编译器的问题(VC6.0),帮忙看一下这个重载操作符为何报错?

showming 2010-09-20 09:47:02
#include<iostream>

using namespace std;

template<class T> class Handle
{
T *ptr;
int *use;
public:
Handle(const T *):ptr=new T(t),use=new int(0){}
Handle(const Handle &);
Handle& operater=(const Handle& );
T& operater*(const Handle *) const;
T* operater&(const Handle &) const;
这三行报错!
~Handle();

};

/*template<class T> Handle<T>::Handle(const Handle &rhs)
{
ptr=rhs.ptr;
*use++;

}

template<class T> Handle<T>& Handle<T>::operater=(const Handle &rhs)
{
*rhs.use++;
if(!*use)
{
delete ptr;
}
ptr=rhs.ptr;
*use--;
use=rhs.use;
}

template<class T> T& Handle<T>::operater*(const Handle *pt) const
{
return pt->*ptr;
}

template<class T> T* Handle<T>::operater&(const Handle &pt) const
{
return pt.ptr;
}

template<class T> Handle<T>::Handle ~Handle()
{
if(--*use)
{delete ptr;delete use;}

}
*/


void main()
{

}


报错信息:--------------------Configuration: handle - Win32 Debug--------------------
Compiling...
handle.cpp
C:\code\handle\handle.cpp(12) : error C2059: syntax error : ';'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(12) : error C2238: unexpected token(s) preceding ';'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(13) : error C2143: syntax error : missing ';' before '*'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(13) : error C2226: syntax error : unexpected type 'Handle<T>'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(13) : error C2238: unexpected token(s) preceding ';'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(14) : error C2143: syntax error : missing ';' before '&'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(14) : error C2372: 'operater' : redefinition; different types of indirection
C:\code\handle\handle.cpp(13) : see declaration of 'operater'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(14) : error C2226: syntax error : unexpected type 'Handle<T>'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
C:\code\handle\handle.cpp(14) : error C2238: unexpected token(s) preceding ';'
C:\code\handle\handle.cpp(17) : see reference to class template instantiation 'Handle<T>' being compiled
Error executing cl.exe.

handle.obj - 9 error(s), 0 warning(s)
...全文
69 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gules 2010-09-20
  • 打赏
  • 举报
回复

Handle(const T *):ptr=new T(t),use=new int(0){} // 改为如下
Handle(const T * t):ptr(new T(*t)), use(new int(0)) {}
chengzhe 2010-09-20
  • 打赏
  • 举报
回复
operator 吧 写错单词了??
  • 打赏
  • 举报
回复

template<class T> T& Handle<T>::operater*(const Handle *pt) const
{
return pt->*ptr;
}


改为

template<class T> T& Handle<T>::operater*(const Handle *pt) const
{
return *(pt->ptr);
}

  • 打赏
  • 举报
回复

template<class T> Handle<T>& Handle<T>::operater=(const Handle &rhs)
{
*rhs.use++;
if(!*use)
{
delete ptr;
}
ptr=rhs.ptr;
*use--;
use=rhs.use;
}

有返回值,你代码里面的return在哪

64,639

社区成员

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

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