一个关于自己定义auto_ptr的问题
帮忙调试一下
///////////////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)
////////////////////////////////////////////////////////////////////