自定义分配器使用向量但不使用deque(包含在队列中)

时间:2013-06-19 13:13:29

标签: c++ stl

我编写了一个自定义分配器,用于在共享内存段中分配内存。

这行代码编译(并运行)很好:

 shp_arr = new (vecmem) vector<shape *,smallocator <shape*> > ;

但这行代码:

shp_queue = new (queuemem) queue< shape *, deque < shape *, smallocator< shape * > > > ;

给了我一些错误。他们在这里:

/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h: In 
instantiation of ‘std::_Deque_base<_Tp, _Alloc>::_Map_alloc_type std::_Deque_base<_Tp, 
_Alloc>::_M_get_map_allocator() const [with _Tp = shape*; _Alloc = smallocator<shape*>;
std::_Deque_base<_Tp, _Alloc>::_Map_alloc_type = smallocator<shape**>]’:
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:549:9:   
required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_map(_Tp**, std::size_t) [with _Tp = 
shape*; _Alloc = smallocator<shape*>; std::size_t = unsigned int]’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:568:4: 
required from ‘std::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = shape*; _Alloc = 
smallocator<shape*>]’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:781:15:     
required from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = shape*; _Alloc = smallocator<shape*>]’
file.cpp:233:30:   required from here
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:529:53: error:  
no matching function for call to ‘smallocator<shape**>::smallocator(const _Tp_alloc_type&)’
/usr/local/lib/gcc/i686-pc-cygwin/4.7.3/../../../../include/c++/4.7.3/bits/stl_deque.h:529:53: note: 
candidates are:
In file included from file.cpp:20:0:
smallocator.hpp:41:3: note: smallocator<T>::smallocator(const smallocator<T>&) [with T = shape**; 
smallocator<T> = smallocator<shape**>]
smallocator.hpp:41:3: note:   no known conversion for argument 1 from ‘const _Tp_alloc_type {aka const 
smallocator<shape*>}’ to ‘const smallocator<shape**>&’
smallocator.hpp:40:3: note: smallocator<T>::smallocator() [with T = shape**]
smallocator.hpp:40:3: note:   candidate expects 0 arguments, 1 provided

smallocator的界面如下所示:

template <typename T>
class smallocator: public std::allocator<T>
  {
    public:
      typedef size_t size_type;
      typedef T* pointer;
      typedef const T* const_pointer;

      template<typename _Tp1>
      struct rebind
        {
          typedef smallocator<_Tp1> other;
        };

      pointer allocate(size_type n, const void *hint=0)
        {
         ... 
        }

      void deallocate(pointer p, size_type n)
        {
         ...
        }

      smallocator() throw(): std::allocator<T>() { std::cout <<"Hello allocator" <<std::endl;}
      smallocator(const smallocator &a) throw(): std::allocator<T>(a) { }
      ~smallocator() throw() { }  
  };

有谁知道问题是什么?谢谢!

1 个答案:

答案 0 :(得分:2)

您没有为smallocator提供以下构造函数:

template <class U>
smallocator(const smallocator<U>& a) throw();

您需要all three

smallocator() throw();
smallocator(const smallocator& a) throw();
template <class U>
smallocator(const smallocator<U>& a) throw();