一个关于自己定义auto_ptr的问题

Noripchen 2005-10-10 10:26:11
帮忙调试一下
///////////////auto_ptr.h/////////////////
template <class Typename>
struct auto_ptr_ref{};

template <class Typename>
class auto_ptr
{
public:
typedef Typename elemtype;

explicit auto_ptr(elemtype *ptr=0) throw();

auto_ptr(auto_ptr&) throw();
template <class T>
auto_ptr(auto_ptr<T>&) throw();

auto_ptr& operator=(auto_ptr&) throw();
template <class T>
auto_ptr& operator=(auto_ptr<T>&) throw();

~auto_ptr() throw();

Typename* get() const throw();
Typename& operator*() const throw();
Typename* operator->() const throw();

Typename* release() throw();
void reset(Typename *ptr=0) throw();

auto_ptr(auto_ptr_ref<Typename>) throw();
auto_ptr& operator=(auto_ptr_ref<Typename>) throw();
template<class T> operator auto_ptr_ref() throw();
template<class T> operator auto_ptr() throw();

};
/////////////////////////////////////////////

////////////////////////////////auto_ptr.cpp///////////
#include "auto_pair.h"

template <class T>
struct auto_ptr_ref
{
T *rp;
auto_ptr_ref(T *ptr=0):rp(ptr){};
};

template <class T>
class auto_ptr
{
private:
T *op;
public:
typedef T elemtype;

explicit auto_ptr(elemtype *ptr=0) throw()
{
op=ptr;
ptr=0;
};

auto_ptr(auto_ptr& e) throw():op(e.release()){};

template <class Y>
auto_ptr(auto_ptr<Y>& e) throw():op(e.release()){};

auto_ptr operator=(auto_ptr& e):op(e.release())
{
return *this;
};
template <class Y>
auto_ptr operator=(auto_ptr<Y>& e) throw():op(e.release())
{
return *this;
};

~auto_ptr() throw()
{
delete op;
}

elemtype* get() const throw()
{
return op;

}
elemtype& operator*() const throw()
{
return *op;
}
elemtype* operator->() const throw()
{
return op;
}

elemtype* release() throw()
{
elemtype *temp(op);
op=0;
return temp;
}

void reset(elemtype *ptr=0) throw();
{
if(op!=ptr)
{
delete op;
op=ptr;
}

}

auto_ptr(auto_ptr_ref<T>& e) throw():op(e.rp);
auto_ptr& operator=(auto_ptr_ref<T>& e) throw():op(e.rp)
{
return *this;
}
template<class U>
operator auto_ptr() throw
{
return auto_ptr<U>(release());
}
template <class U>
operator auto_ptr_ref() throw()
{
return auto_ptr_ref<U>(release());
}


};
//////////////////////////////////////////////////////////////

/////////////////////////////////debug//////////////////////////
--------------------Configuration: auto_pair - Win32 Debug--------------------
Compiling...
auto_pair.cpp
f:\c++\auto_pair\auto_pair.h(14) : error C2535: '__thiscall auto_ptr<T>::auto_ptr<T>(class auto_ptr<T> &)' : member function already defined or declared
f:\c++\auto_pair\auto_pair.h(12) : see declaration of 'auto_ptr<Typename>::auto_ptr<Typename>'
f:\c++\auto_pair\auto_pair.h(34) : see reference to class template instantiation 'auto_ptr<Typename>' being compiled
f:\c++\auto_pair\auto_pair.h(18) : error C2535: 'class auto_ptr<T> &__thiscall auto_ptr<T>::operator =(class auto_ptr<T> &)' : member function already defined or declared
f:\c++\auto_pair\auto_pair.h(16) : see declaration of '='
f:\c++\auto_pair\auto_pair.h(34) : see reference to class template instantiation 'auto_ptr<Typename>' being compiled
f:\c++\auto_pair\auto_pair.cpp(8) : error C2953: 'auto_ptr_ref' : template class has already been defined
f:\c++\auto_pair\auto_pair.cpp(8) : see declaration of 'auto_ptr_ref'
f:\c++\auto_pair\auto_pair.cpp(92) : error C2953: 'auto_ptr' : template class has already been defined
f:\c++\auto_pair\auto_pair.cpp(92) : see declaration of 'auto_ptr'
Error executing cl.exe.

auto_pair.obj - 4 error(s), 0 warning(s)
////////////////////////////////////////////////////////////////////
...全文
120 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlsue 2005-10-10
  • 打赏
  • 举报
回复
回楼上,应该可以这么说。虽说这是标准的。但到2002年才有一家编译器厂商实现出来,估计到现在也没有几家。所以还是不推荐了。这里改过。
doway 2005-10-10
  • 打赏
  • 举报
回复
上次听一老大说,目前还没有任何编译器实现了 export,才过了没几天。:)
xlsue 2005-10-10
  • 打赏
  • 举报
回复
如果你的编译器支持模板较好,你还可以这样试试:
export
template <class Typename>
class auto_ptr
{
...
};
xlsue 2005-10-10
  • 打赏
  • 举报
回复
楼主用什么编译器?
xlsue 2005-10-10
  • 打赏
  • 举报
回复
把#include "auto_ptr.cpp"放到头文件中试试。
v41dugu 2005-10-10
  • 打赏
  • 举报
回复
不能分开编译?。。。为什么呀?
qhfu 2005-10-10
  • 打赏
  • 举报
回复
模板不能分开编译 ! 把两个放一起再试试吧!~

64,681

社区成员

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

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