Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compile errors with list of auto_ptr

Reply
Thread Tools

compile errors with list of auto_ptr

 
 
gg
Guest
Posts: n/a
 
      08-10-2005
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,
^^^^^^^^^^^^^^^^^^

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-10-2005
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
 
Reply With Quote
 
 
 
 
ravinderthakur@gmail.com
Guest
Posts: n/a
 
      08-11-2005
you are trying to store a pointer to std::list in an auto_ptr where as
auto_ptr works only with pointers.

thanks
rt

 
Reply With Quote
 
garth_rockett@yahoo.com
Guest
Posts: n/a
 
      08-11-2005

> 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

 
Reply With Quote
 
garth_rockett@yahoo.com
Guest
Posts: n/a
 
      08-11-2005
> 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.

 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      08-11-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit auto_ptr<T>::auto_ptr(T*) ? Sousuke C++ 9 03-16-2010 11:54 AM
auto_ptr compile error George2 C++ 0 03-21-2008 03:00 AM
compile error about auto_ptr George2 C++ 0 02-23-2008 02:10 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
auto_ptr<Derived> to auto_ptr<Base> Siemel Naran C++ 2 01-11-2005 04:45 AM



Advertisments