problem with auto_ptr<> and assigning derived class to base class object

S

Stephan Hoffmann

Hi,

I'm new to std::auto_ptr<> and wanted to use it with
a base class and several derived classes.

I'm using gcc 3.3.5 and get a compile error I don't know
how to resolve when compiling the following (in file testInteger.cc):

line 38: std::auto_ptr<HInteger> d(new HInteger(5));
line 39: std::auto_ptr<HObject> e(new HObject());
line 40: e= d;

Here is the error
/usr/include/c++/3.3.5/memory: In member function `std::auto_ptr<_Tp>&
std::auto_ptr<_Tp>::eek:perator=(std::auto_ptr<_Tp1>&) [with _Tp1 =
HInteger,
_Tp = HObject]':
testInteger.cc:40: instantiated from here
/usr/include/c++/3.3.5/memory:237: error: no matching function for call to `
std::auto_ptr<HObject>::reset(HInteger*)'
/usr/include/c++/3.3.5/memory:316: error: candidates are: void
std::auto_ptr<_Tp>::reset(_Tp*) [with _Tp = HObject]

HInteger is derived from HNumber which is derived from HObject.

As I'm not familiar with auto_ptr, I tested this with a trivial
example: a class Base and a class Derived and all of the following
compiles fine:

std::auto_ptr<Base> a(new Base());
std::auto_ptr<Derived> b(new Derived());
std::auto_ptr<Base> c(new Derived());
a= b;

Originally, my 'root' class HObject was abstract (had pure virtual members),
but removing that didn't change this error message.

So I guess one of my classes HObject, HNumber, or HInteger is missing
a member function, but I don't know what that could be. I do not
hide any of the functions that may be constructed by the compiler
(default or copy c'tor, assignment operator). The destructor is
virtual and public in all 3 classes.

Any hint appreciated, thanks,

Stephan
 
N

Neelesh Bodas

Stephan said:
Hi,

I'm new to std::auto_ptr<> and wanted to use it with
a base class and several derived classes.

I'm using gcc 3.3.5 and get a compile error I don't know
how to resolve when compiling the following (in file testInteger.cc):

line 38: std::auto_ptr<HInteger> d(new HInteger(5));
line 39: std::auto_ptr<HObject> e(new HObject());
line 40: e= d;

Here is the error
/usr/include/c++/3.3.5/memory: In member function `std::auto_ptr<_Tp>&
std::auto_ptr<_Tp>::eek:perator=(std::auto_ptr<_Tp1>&) [with _Tp1 =
HInteger,
_Tp = HObject]':
testInteger.cc:40: instantiated from here
/usr/include/c++/3.3.5/memory:237: error: no matching function for call to `
std::auto_ptr<HObject>::reset(HInteger*)'
/usr/include/c++/3.3.5/memory:316: error: candidates are: void
std::auto_ptr<_Tp>::reset(_Tp*) [with _Tp = HObject]

HInteger is derived from HNumber which is derived from HObject.

Assuming that the derivation is public, this code compiles well on
comeau online, also on g++ 3.4.2. May be you need to upgrade the
compiler.
 
S

Stephan Hoffmann

Hi,

I found the problem, it didn't have anything to do with auto_ptr.
I had mistakenly derived HInteger from HObjectPtr, which is
a typedef for std::auto_ptr<HObject>, instead of HObject.

Thanks, Stephan
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top