By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

compile errors with list of auto_ptr

P: n/a
gg
I am getting the following compilation errors with the following
program. My compiler is aCC 03.27 on HP-UX11 -

#include <iostream>
using namespace std;

#include <list>
#include <memory>
#include <string>

int main ( void )
{
auto_ptr < list < auto_ptr < string > > > list1;
auto_ptr < list < auto_ptr < string > > > list2;

auto_ptr < string > strAPT1 (new string ( "a" ) );
auto_ptr < string > strAPT2 (new string ( "b" ) );
auto_ptr < string > strAPT3 (new string ( "c" ) );

list1->push_back ( strAPT1 );
list1->push_back ( strAPT2 );
list1->push_back ( strAPT3 );
}
Error 226: "/opt/aCC/include_std/memory", line 187 # No appropriate
function found for call of 'auto_ptr::auto_ptr'. Last viable
candidate was
"std::auto_ptr<std::basic_string<char,std::char_tr aits<char>,std::allocator<char>
::auto_ptr(std::basic_string<char,std::char_trait s<char>,std::allocator<char> > *)" ["/opt/aCC/include_std/memory", line 830]. Argument of type 'const
std::auto_ptr<std::basic_string<char,std::char_tra its<char>,std::allocator<char>
&' could not be converted to
'std::basic_string<char,std::char_traits<char>,std ::allocator<char> >
*'.
new (__p) _TypeT (__val);
^^^^^^^^^^^^^^^^^^^^^^^^
Error 556: "/opt/aCC/include_std/list", line 911 # Unable to generate
specialization "void

__rw::__rw_construct<std::auto_ptr<std::basic_stri ng<char,std::char_traits<char>,std::allocator<char > ,std::auto_ptr<std::basic_string<char,std::char_t raits<char>,std::allocator<char> > > (std::auto_ptr<std::basic_string<char,std::char_t raits<char>,std::allocator<char> > > *,const
std::auto_ptr<std::basic_string<char,std::char_tra its<char>,std::allocator<char> &)" due to errors during generation. _RWSTD_VALUE_ALLOC (_C_value_alloc_type,
^^^^^^^^^^^^^^^^^^
Error 556: "/opt/aCC/include_std/list", line 911 # Unable to generate
specialization "void

__rw::__rw_construct<std::auto_ptr<std::basic_stri ng<char,std::char_traits<char>,std::allocator<char > ,std::auto_ptr<std::basic_string<char,std::char_t raits<char>,std::allocator<char> > > (std::auto_ptr<std::basic_string<char,std::char_t raits<char>,std::allocator<char> > > *,const


std::auto_ptr<std::basic_string<char,std::char_tra its<char>,std::allocator<char> &)" due to errors during generation.

_RWSTD_VALUE_ALLOC (_C_value_alloc_type,
^^^^^^^^^^^^^^^^^^

Aug 10 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
gg wrote:
I am getting the following compilation errors with the following
program. My compiler is aCC 03.27 on HP-UX11 -

#include <iostream>
using namespace std;

#include <list>
#include <memory>
#include <string>

int main ( void )
{
auto_ptr < list < auto_ptr < string > > > list1;
'std::auto_ptr' does not satisfy the requirements for objects to be
contained in standard containers ('std::list' included).

Get Boost and use 'boost::shared_ptr' for that.
[...]


V
Aug 10 '05 #2

P: n/a
you are trying to store a pointer to std::list in an auto_ptr where as
auto_ptr works only with pointers.

thanks
rt

Aug 11 '05 #3

P: n/a
auto_ptr < list < auto_ptr < string > > > list1;
auto_ptr < list < auto_ptr < string > > > list2;


You should not try to store auto_ptr objects in STL containers.
auto_ptr copy semantics are different from normal objects. Copying
mutates the source auto_ptr and sets the underlying pointer to NULL.
This algorithms on the container which depend on temporary copying of
objects can fail on auto_ptr containers. An auto_ptr copy constructor /
assignment operator prototype too is slightly different:

auto_ptr(auto_ptr&); // see no X(const X&) but X(X&)

If you have a list< auto_ptr<...> > and call insert on it with a new
auto_ptr object, it won't compile. So the above code is incorrect and
'auto_ptr's are not meant to be stored in STL containers.

Cheers,
Andy

Aug 11 '05 #4

P: n/a
> you are trying to store a pointer to std::list in an auto_ptr where as
auto_ptr works only with pointers.


I guess if you really ever allocate an std::list on the heap, you could
use an auto_ptr to manage its heap-life. There is not problem there.

Aug 11 '05 #5

P: n/a

ga***********@yahoo.com schreef:
auto_ptr < list < auto_ptr < string > > > list1;
auto_ptr < list < auto_ptr < string > > > list2;


You should not try to store auto_ptr objects in STL containers.
auto_ptr copy semantics are different from normal objects. Copying
mutates the source auto_ptr and sets the underlying pointer to NULL.
This algorithms on the container which depend on temporary copying of
objects can fail on auto_ptr containers. An auto_ptr copy constructor /
assignment operator prototype too is slightly different:

auto_ptr(auto_ptr&); // see no X(const X&) but X(X&)

If you have a list< auto_ptr<...> > and call insert on it with a new
auto_ptr object, it won't compile. So the above code is incorrect and
'auto_ptr's are not meant to be stored in STL containers.


Your explanation why it won't work is perfect, but let me point out
that it's not just insert() that prevents it. Every function in list
can break, including the default constructor! Implementations differ
slightly, so you cannot say precisely which functions will break.

Regards,
Michiel Salters

Aug 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.