Help, please!! <auto_ptr> & VC7 problem!

E

Evgeny

Hi, all!
I try to convert my existing code written in VC6 to VC7 and have some
problem with stl auto pointer template.

class CColumn;
#include <memory>
typedef auto_ptr<CMyBase> CMyBasePtr;

class CMyBase
{
virtual CMyBase* Clone(void) const
{ return MyBasePtr(new MyBase()); }
}

class CMyTest : public MyBase
{
virtual CMyBase* Clone(void) const
{
return MyBasePtr(new CMyTest()); - this line detected as wrong and i
get compiler message C2440: 'return' : cannot convert from
'std::auto_ptr<_Ty>' to 'CMyBase*'
}
}

If i change to follow implementation a get other error C2558

class CMyBase
{
virtual CMyBasePtr Clone(void) const
{ return new MyBase());}
}

class CMyTest : public MyBase
{
virtual CMyBase* Clone(void) const
{
return MyBasePtr(new CMyTest()); - this line detected as wrong and i
get compiler message C2558: class 'std::auto_ptr<_Ty>' : no copy constructor
available or copy constructor is declared 'explicit
}
}

Where is a problem?? It's compiled fine in VC6!

Thanks in advance,
Evgeny
 
C

Carl Daniel [VC++ MVP]

Evgeny said:
Hi, all!
I try to convert my existing code written in VC6 to VC7 and have some
problem with stl auto pointer template.

class CColumn;
#include <memory>

you probably had a using namespace std in here too.
typedef auto_ptr<CMyBase> CMyBasePtr;

class CMyBase
{
virtual CMyBase* Clone(void) const
{ return MyBasePtr(new MyBase()); }

Why is the function defined as returning CMyBase* if you want to return an
auto_ptr?
}

class CMyTest : public MyBase
{
virtual CMyBase* Clone(void) const
{
return MyBasePtr(new CMyTest()); - this line detected as wrong
and i get compiler message C2440: 'return' : cannot convert from
'std::auto_ptr<_Ty>' to 'CMyBase*'
}
Likewise?

}

If i change to follow implementation a get other error C2558

class CMyBase
{
virtual CMyBasePtr Clone(void) const
{ return new MyBase());}
}

class CMyTest : public MyBase
{
virtual CMyBase* Clone(void) const
{
return MyBasePtr(new CMyTest()); - this line detected as wrong
and i get compiler message C2558: class 'std::auto_ptr<_Ty>' : no
copy constructor available or copy constructor is declared 'explicit
Likewise?

}
}

Where is a problem?? It's compiled fine in VC6!

VC6 had a seriously non-conformant implementation of std::auto_ptr. Try
something like this:

#include <memory>
using std::auto_ptr;

class CMyBase;
typedef auto_ptr<CMyBase> CMyBasePtr;

class CMyBase
{
virtual CMyBasePtr Clone(void) const
{
return CMyBasePtr(new CMyBase());
}
};

class CMyTest : public CMyBase
{
virtual CMyBasePtr Clone(void) const
{
return CMyBasePtr(new CMyTest());
}
};

-cd
 
E

Evgeny

thanks, but it doesn't solve a problem....

Carl Daniel said:
you probably had a using namespace std in here too.


Why is the function defined as returning CMyBase* if you want to return an
auto_ptr?


VC6 had a seriously non-conformant implementation of std::auto_ptr. Try
something like this:

#include <memory>
using std::auto_ptr;

class CMyBase;
typedef auto_ptr<CMyBase> CMyBasePtr;

class CMyBase
{
virtual CMyBasePtr Clone(void) const
{
return CMyBasePtr(new CMyBase());
}
};

class CMyTest : public CMyBase
{
virtual CMyBasePtr Clone(void) const
{
return CMyBasePtr(new CMyTest());
}
};

-cd
 
E

Evgeny

It's strange. I've create new console application project and add my simple
classes.
In this project it already compiled!
Possible in my really project previouse includes generates a problem?
 
E

Evgeny

I found, that a problem occured, when CMyBasePtr is a member of another stl
map, defined as follow
typedef map<int, CMyBasePtr> MapBase;
itr->second - generates compiler error C2558: class 'std::auto_ptr<_Ty>' :
no copy constructor available or copy constructor is declared 'explicit
see follow code:

#include <memory>
class CMyBase;
typedef auto_ptr<CMyBase> CMyBasePtr;
MapBase my_map;
MapBase::iterator itr = my_map.begin();
itr->second - compiler error C2558

Where is a problem, please?
What should i defined in addition?
Thanks in advance
 
C

Carl Daniel [VC++ MVP]

Evgeny said:
I found, that a problem occured, when CMyBasePtr is a member of
another stl map, defined as follow
typedef map<int, CMyBasePtr> MapBase;
itr->second - generates compiler error C2558: class
'std::auto_ptr<_Ty>' : no copy constructor available or copy
constructor is declared 'explicit

std::auto_ptr<T> is not suitable for use in any standard containers. This
is because std::auto_ptr<T> does not meet the CopyConstructible requirement
of the standard containers. For a suitable replacement, see
boost::shared_ptr<T> at www.boost.org.

-cd
 

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