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

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

P: n/a
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>::operator=(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
Dec 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Stephan Hoffmann wrote:
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>::operator=(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.

Dec 11 '05 #2

P: n/a
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

Stephan Hoffmann wrote:
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>::operator=(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


Dec 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.