ptr_map<int, T> where T has no default constructor because its private members are const
hi, i can build a map<int,A*>, but not a boost::ptr_map<unsigned int,A>. could someone please help? thank you, e. #include <boost/ptr_container/ptr_map.hpp> using std::map; using std::cout; using std::endl; class A{ public: A(double x_):x(x_){}; const double x; }; int main(){ map<int,A*> map_A; //cannot use map<int,A> //because A() not defined //why? because A has const members. A a(0.0); map_A[0]=new A(0.0);//fine boost::ptr_map<unsigned int,A> ptr_map_A; ptr_map_A[0]=new A(0.0);//not fine return 0; }; **** Build of configuration Debug for project testing_map **** make -k all Building file: ../main.cpp Invoking: GCC C++ Compiler g++ -I/usr/local/boost_1_34_1 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp" ../main.cpp:25:3: warning: no newline at end of file ../main.cpp: In function ‘int main()’: ../main.cpp:22: error: no match for ‘operator=’ in ‘ptr_map_A.boost::ptr_map<unsigned int, A, std::less<unsigned int>, boost::heap_clone_allocator, std::allocator<std::pair<const unsigned int, void*> > >::<anonymous>.boost::ptr_map_adapter<A, std::map<unsigned int, void*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, void*> > >, boost::heap_clone_allocator>::<anonymous>.boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator>::operator[] [with T = A, VoidPtrMap = std::map<unsigned int, void*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, void*> > >, CloneAllocator = boost::heap_clone_allocator](((const unsigned int&)((const unsigned int*)(&0u)))) = (((A*)operator new(8u)), (<anonymous>->A::A(0.0), <anonymous>))’ ../header.h:4: note: candidates are: A& A::operator=(const A&) /usr/local/boost_1_34_1/boost/ptr_container/ptr_map_adapter.hpp: In member function ‘typename boost::ptr_container_detail::associative_ptr_container<boost::ptr_container_detail::map_config<T, VoidPtrMap>, CloneAllocator>::reference boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator>::insert_lookup(const typename boost::ptr_container_detail::associative_ptr_container<boost::ptr_container_detail::map_config<T, VoidPtrMap>, CloneAllocator>::key_type&) [with T = A, VoidPtrMap = std::map<unsigned int, void*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, void*> > >, CloneAllocator = boost::heap_clone_allocator]’: /usr/local/boost_1_34_1/boost/ptr_container/ptr_map_adapter.hpp:268: instantiated from ‘typename boost::ptr_container_detail::associative_ptr_container<boost::ptr_container_detail::map_config<T, VoidPtrMap>, CloneAllocator>::reference boost::ptr_container_detail::ptr_map_adapter_base<T, VoidPtrMap, CloneAllocator>::operator[](const typename boost::ptr_container_detail::associative_ptr_container<boost::ptr_container_detail::map_config<T, VoidPtrMap>, CloneAllocator>::key_type&) [with T = A, VoidPtrMap = std::map<unsigned int, void*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, void*> > >, CloneAllocator = boost::heap_clone_allocator]’ ../main.cpp:22: instantiated from here /usr/local/boost_1_34_1/boost/ptr_container/ptr_map_adapter.hpp:168: error: no matching function for call to ‘A::A()’ ../header.h:6: note: candidates are: A::A(double) ../header.h:4: note: A::A(const A&) make: *** [main.o] Error 1 make: Target `all' not remade because of errors. Build complete for project testing_map
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of e r Sent: Monday, August 13, 2007 6:23 PM To: boost-users@lists.boost.org Subject: [Boost-users] ptr_map<int, T> where T has no default constructor because its private members are const
hi,
i can build a map<int,A*>, but not a boost::ptr_map<unsigned int,A>. could someone please help?
thank you,
e.
#include <boost/ptr_container/ptr_map.hpp>
using std::map; using std::cout; using std::endl;
class A{ public: A(double x_):x(x_){}; const double x; };
int main(){
map<int,A*> map_A; //cannot use map<int,A> //because A() not defined //why? because A has const members.
A a(0.0); map_A[0]=new A(0.0);//fine
boost::ptr_map<unsigned int,A> ptr_map_A; ptr_map_A[0]=new A(0.0);//not fine
return 0; };
I don't know for sure, but this may be disallowed for exception safety. In any case this should work: unsigned int key = 0; ptr_map_A.insert(key, new A(0.0)); "insert" also has the advantage in that the map doesn't have to create an empty value or null-pointer entry) before the assignment. -- Bill --
participants (2)
-
Bill Buklis
-
e r