我想随机分配给 boost::ptr_vector。使用正常的 STL 向量我会这样做:
class A{
};
A* a1 = new A();
A* a2 = new A();
vector<A*> normal_vec;
normal_vec.resize(2);
normal_vec[1] = a1;
normal_vec[0] = a2;
我正在尝试对 boost::ptr_vector 做同样的事情:
A* a1 = new A();
A* a2 = new A();
ptr_vector<A> boost_vec;
boost_vec.resize(2);
boost_vec[1] = a1;
boost_vec[0] = a2;
但我收到以下错误:
no match for ‘operator=’ in ‘v.boost::ptr_vector<A>::<anonymous>.boost::ptr_sequence_adapter<T, VoidPtrSeq, CloneAllocator>::operator[] [with T = A, VoidPtrSeq = std::vector<void*, std::allocator<void*> >, CloneAllocator = boost::heap_clone_allocator, boost::ptr_sequence_adapter<T, VoidPtrSeq, CloneAllocator>::reference = A&, boost::ptr_sequence_adapter<T, VoidPtrSeq, CloneAllocator>::size_type = long unsigned int](1ul) = a1’
但我很难理解这一点