std::auto_ptr error

G

Guido Forthofer

Hello,

I convert my VC6 C++ project after VC7 (7.1) and have now an compiler
error

d:\Programme\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810):
error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available
or copy constructor is declared 'explicit'
with
[
_Ty=KServer
]

Here as example my code

class KSrv
{
...
}:

typedef std::auto_ptr< KSrv > TySrvPtr;
typedef std::vector< TySrvPtr > TySrvList;
TySrvList oList;
....

oList.push_back(TySrvPtr(new KSrv)); <-- the compiler throw the error
message

why???

Guido
 
A

Arnold the Aardvark

Here as example my code
class KSrv
{
..
}:

typedef std::auto_ptr< KSrv > TySrvPtr;
typedef std::vector< TySrvPtr > TySrvList;
TySrvList oList;
...

oList.push_back(TySrvPtr(new KSrv)); <-- the compiler throw the error
message

why???

It is an error to use std::auto_ptr in STL containers. It does not
have the correct copy semantics - pointer ownership is transferred,
not shared. Have a look into the Boost library's class shared_ptr.


Arnold the Aardvark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top