0

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

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

 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() { }  
  };

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

4

1 回答 1

3

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

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

你需要所有三个

smallocator() throw();
smallocator(const smallocator& a) throw();
template <class U>
smallocator(const smallocator<U>& a) throw();
于 2013-06-19T13:43:19.603 回答