关于allocator类

adventurelw 2009-05-07 03:11:55
也许不是这个类的问题,但目前还只能认为是它的问题
还是实现Vector模版类,代码如下,有点长,跟书上应该是没啥区别的

#ifndef TEMPLATE_H_
#define TEMPLATE_H_
#include <algorithm>
#include <memory>
#include <cstddef>

template <typename T>
class Vector
{
public:
typedef T * iterator;
typedef std::size_t size_type;
private:
static std::allocator<T> alloc;//一个静态对象所有Vector<T>对象可以公用,调用相应的成员函数分配不同的空间
iterator element;
iterator first_free;
iterator end;
void destory();//销毁元素及释放内存
template <typename iter>
void copy_elem(iter b, iter e);//复制元素
void reallocate();//重新分配足够空间,一般是原来空间的两倍
public:
Vector() : element(0), first_free(0), end(0) {}
template <typename iter>
Vector(iter b, iter e);//使用同类模版构造
Vector(size_type n, const T &t);
Vector(const Vector &vec);//复制构造
~Vector() { destory(); }
Vector &operator =(const Vector &vec);//赋值重载
void push_back(const T &t);
size_type size() const { return first_free - element; }
void resize(size_type new_size, const T &t)
{
if(size() < new_size)
{
if(capacity() < new_size)
reallocate();
std::uninitialized_fill(first_free, element + new_size, t);
}
else
{
T *p = first_free;
while(p != element + new_size)
alloc.destroy(--p);
}
first_free = element + new_size;
}
void reserved(size_type n);//重新分配空间
size_type capacity() const { return end - element; }
T &operator [](size_type i) { return *(element + i); }
const T &operator [](size_type i) const { return *(element + i); }
};

template <typename T>
void Vector<T>::destory()
{
for(T *p = first_free; p != element;)
alloc.destroy(--p);
if(element)
alloc.deallocate(element, end - element);
}

template <typename T> template <typename iter>
void Vector<T>::copy_elem(iter b, iter e)
{
for(size_type i = 0; i < (e - b); ++i)
alloc.construct(element + i, *(b + i));
}

template <typename T>
void Vector<T>::reallocate()
{
size_type sizenow = size();
size_type newcap = 2 * std::max<size_type>(sizenow, 1);
T *newelement = alloc.allocate(newcap);
std::uninitialized_copy(element, first_free, newelement);
destory();
element = newelement;
first_free = element + sizenow;
end = element + newcap;
}

template <typename T> template <typename iter>
Vector<T>::Vector(iter b, iter e)
: element(alloc.allocate((e - b) * 3 / 2)), first_free(element + (e - b)), end(element + (e - b) * 3 / 2)
{
copy_elem(b, e);
}

template <typename T>
Vector<T>::Vector(typename Vector<T>::size_type n, const T &t)
: element(alloc.allocate(n * 3 / 2)), first_free(element + n), end(element + n * 3 / 2)
{
for(size_type i = 0; i < n; ++i)
alloc.construct(element + i, t);
}

template <typename T>
Vector<T>::Vector(const Vector<T> &vec)
: element(alloc.allocate(vec.capacity())), first_free(element + vec.size()), end(element + vec.capacity())
{
copy_elem(vec.element, vec.first_free);
}

template <typename T>
Vector<T> &Vector<T>::operator =(const Vector<T> &vec)
{
if(this != &vec)
{
destory();
element = alloc.allocate(vec.capacity());
first_free = element + vec.size();
end = element + vec.capacity();
copy_elem(vec.element, vec.first_free);
}
return *this;
}

template <typename T>
void Vector<T>::push_back(const T &t)
{
if(first_free == end)
reallocate();
alloc.construct(first_free, t);
++first_free;
}

template <typename T>
void Vector<T>::reserved(size_type n)
{
T *newelement = alloc.allocate(n);
size_type size_now = size();
std::uninitialized_copy(element, first_free, newelement);
destory();
element = newelement;
first_free = element + size_now;
end = element + n;
}
#endif


上边代码,如果main函数中不包括对这个类的使用的话,编译是没有问题的,但一旦构造了对象,进行操作,就出问题了,简单操作代码如下:

#include "stdafx.h"
#include "template.h"

const std::size_t SIZE = 12;
int main()
{
Vector<int> ivec;
int n;
while(std::cin >> n)
ivec.push_back(n);
for(Vector<int>::size_type i = 0; i < ivec.size(); ++i)
std::cout << ivec[i] << " ";
std::cout << std::endl;
return 0;
}

错误如下:
错误 2 error LNK2001: 无法解析的外部符号 "private: static class std::allocator<int> Vector<int>::alloc" (?alloc@?$Vector@H@@0V?$allocator@H@std@@A) test1.obj test1
错误 3 fatal error LNK1120: 1 个无法解析的外部命令 D:\My document\Visual Studio 2008\Projects\test1\Debug\test1.exe test1
这个错误提示以前大概见过类似的,但从来不明白啥意思,也不知道问题在哪,恳请大家帮忙解惑,万分感谢。
...全文
278 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
胖树苗 2009-07-28
  • 打赏
  • 举报
回复
谢谢,增加了点知识
Qlaiaqu 2009-05-07
  • 打赏
  • 举报
回复
template<typename T>
std::allocator<T> Vector<T>::alloc;
lingyin55 2009-05-07
  • 打赏
  • 举报
回复
private:
static std::allocator<T> alloc;///这个只是声明,你还需要在类体外定义
sjllwj520 2009-05-07
  • 打赏
  • 举报
回复
是编译器被混淆了
(?alloc@?$Vector@H@@0V?$allocator@H@std@@A)
他问你是调用alloc of Vector 或是 std lib 里的alloc
说明你定义的Template 未调用完整的namespace
所以编译时语法正确所以不给错误
而实例是,linker 找不到正确的function to call 故而报错。

解决方案是
添加

#include <vector>
using namespace std;
来使调用明确化
adventurelw 2009-05-07
  • 打赏
  • 举报
回复
我晕倒,这回,的确是static的原因。。。。
yshuise 2009-05-07
  • 打赏
  • 举报
回复
一般情况下是这样用,展现模板的可配置性:
template<T, class C = std::allocator<T> >
class vector;
yshuise 2009-05-07
  • 打赏
  • 举报
回复
static需要在类外面定义。
 typename std::allocator<T>  Vector<T>::alloc;

yshuise 2009-05-07
  • 打赏
  • 举报
回复
static需要在类外面定义。
 typename std::allocator<T>  Vector<T>::alloc;

64,648

社区成员

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

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