模仿编写的智能指针template有点问题,麻烦大神们看一下

Qinyng 2014-09-20 12:26:47
我模仿的template要求实现复制时采用深复制

template<typename T>
class t_ptr
{
public:
explicit t_ptr(const int&);
explicit t_ptr(const T *,const int&); ///Notice:deep copy
explicit t_ptr(const t_ptr<T> &_copyobj); ///Notice:deep copy
~t_ptr() { delete []_ptr; }

void clear();
int copy(const T *,const int&);
int copy(const t_ptr &);
int length() const { return _size; }
T* ptr() { return _ptr; }
void swap(T* _exchange);

private:
int _size;
T *_ptr;
};


template<typename T> t_ptr<T>::t_ptr(const int& _newlen)
{
_size = 0;
_ptr = NULL;

if(_ptr = new T[_newlen])
_size = _newlen;
else
throw std::runtime_error("New TempPtr Object Failed");
}

template<typename T> t_ptr<T>::t_ptr(const T* _src,const int& _srclen)
{
_size = 0;
_ptr = NULL;

if(0 == _srclen || NULL == _src)
return;

if(_ptr = new T[_srclen])
_size = _srclen;
else
throw std::runtime_error("New TempPtr Object Failed");

memcpy(_ptr,_src,_srclen);
}

template<typename T> t_ptr<T>::t_ptr(const t_ptr<T> &_src)
{
_size = 0;
_ptr = NULL;

if(_ptr = new T[_src.length()])
_size = _src.length();
else
throw std::runtime_error("New TempPtr Object Failed");

memset(_ptr,_src.ptr(),_size);
}

template<typename T> void t_ptr<T>::clear()
{
if(0 == _size)
return;

delete []_ptr;
_size = 0;
}

template<typename T> int t_ptr<T>::copy(const T* _src,const int& _srclen)
{
if(0 == _srclen || NULL == _src)
return -1;

T *_temp = new T[_srclen];
memcpy(_temp,_src,_srclen);

this->swap(_temp);
_size = _srclen;

delete []_temp;
return _size;
}

template<typename T> int t_ptr<T>::copy(const t_ptr &_src)
{
if(0 == _src.length())
return -1;

int _srclen = _src.length();
T *_temp = new T[_srclen];
memcpy(_temp,_src.ptr(),_srclen);

this->swap(_temp);
_size = _srclen;
delete []_temp;
return _size;
}

template<typename T> void t_ptr<T>::swap(T* _exchange)
{
T *_tempex = _ptr;
_ptr = _exchange;
_exchange = _tempex;
}


错误代码:(vs2012)
main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall t_ptr<char>::t_ptr<char>(int const &)" (??0?$t_ptr@D@@QAE@ABH@Z),该符号在函数 _main 中被引用
1>E:\C++\NULL\template_alloc_ptr\Debug\template_alloc_ptr.exe : fatal error LNK1120: 1 个无法解析的外部命令


另外再问一下:
const int & 会变成int const &,应该怎么解决??
...全文
190 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级能量泡泡 2014-09-29
  • 打赏
  • 举报
回复
引用 5 楼 Qinyng 的回复:
话说const int & 会变成int const &,应该怎么解决??
本质一样 后者更符合类型+修饰符的语言约定,前者比较符合多数人的习惯。
ri_aje 2014-09-22
  • 打赏
  • 举报
回复
引用 5 楼 Qinyng 的回复:
话说const int & 会变成int const &,应该怎么解决??
你先把问题说清楚吧,const int& 和 int const& 根本就是一个东西,有什么变不变的,更何谈怎么解决。
我看你有戏 2014-09-20
  • 打赏
  • 举报
回复


#include "iostream"
using namespace std;



template<typename T>
class t_ptr
{
public:
	explicit t_ptr(const int&);
	explicit t_ptr(const T *,const int&);		///Notice:deep copy
	explicit t_ptr(const t_ptr<T> &_copyobj);	///Notice:deep copy
	~t_ptr() { delete []_ptr; }

	void clear();
	int copy(const T *,const int&);
	int copy(const t_ptr &);
	int length() const { return _size; }
	T* ptr() { return _ptr; }
	void swap(T* _exchange);

private:
	int _size;
	T *_ptr;
};

template<typename T> t_ptr<T>::t_ptr(const int& _newlen)
{
	_size = 0;
	_ptr = NULL;

	if(_ptr = new T[_newlen])
		_size = _newlen;
	else
		throw std::runtime_error("New TempPtr Object Failed");
}

template<typename T> t_ptr<T>::t_ptr(const T* _src,const int& _srclen)
{
	_size = 0;
	_ptr = NULL;

	if(0 == _srclen || NULL == _src)
		return;

	if(_ptr = new T[_srclen])
		_size = _srclen;
	else
		throw std::runtime_error("New TempPtr Object Failed");

	memcpy(_ptr,_src,_srclen);
}

template<typename T> t_ptr<T>::t_ptr(const t_ptr<T> &_src)
{
	_size = 0;
	_ptr = NULL;

	if(_ptr = new T[_src.length()])
		_size = _src.length();
	else
		throw std::runtime_error("New TempPtr Object Failed");

	memset(_ptr,_src.ptr(),_size);
}

template<typename T> void t_ptr<T>::clear()
{
	if(0 == _size)
		return;

	delete []_ptr;
	_size = 0;
}

template<typename T> int t_ptr<T>::copy(const T* _src,const int& _srclen)
{
	if(0 == _srclen || NULL == _src)
		return -1;

	T *_temp = new T[_srclen];
	memcpy(_temp,_src,_srclen);

	this->swap(_temp);
	_size = _srclen;

	delete []_temp;
	return _size;
}

template<typename T> int t_ptr<T>::copy(const t_ptr &_src)
{
	if(0 == _src.length())
		return -1;

	int _srclen = _src.length();
	T *_temp = new T[_srclen];
	memcpy(_temp,_src.ptr(),_srclen);

	this->swap(_temp);
	_size = _srclen;
	delete []_temp;
	return _size;
}

template<typename T> void t_ptr<T>::swap(T* _exchange)
{
	T *_tempex = _ptr;
	_ptr = _exchange;
	_exchange = _tempex;
}


int main()
{
	t_ptr<char> _temp(5);
	system("pause");
	return 0;
}

win7 32位 vs2003.net 测试通过 微软不厚道,vs越升越大,越奇怪,还是vc6,vs2003.net 经典,小而精
我看你有戏 2014-09-20
  • 打赏
  • 举报
回复
摸办类需要放在一个文件里面的,不能分开
Qinyng 2014-09-20
  • 打赏
  • 举报
回复
对了,还有main.cpp

int main()
{
	t_ptr<char> _temp(5);
	return 0;
}
Qinyng 2014-09-20
  • 打赏
  • 举报
回复
话说const int & 会变成int const &,应该怎么解决??
Qinyng 2014-09-20
  • 打赏
  • 举报
回复
额,template不能放在两个文件中吗??额,好吧

64,687

社区成员

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

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