473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
2 4539

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Siemel Naran | last post by:
This code fails compile std::auto_ptr<Base> f() { std::auto_ptr<Derived> out(new Derived()); return out; } There is ambiguity between a templated constructor and templated operator...
4
by: Kurt Stutsman | last post by:
I am developing a type that protects ownership of data with non-const ref copy-constructor and assignment operator similiar to auto_ptr<>. I read that auto_ptr<> uses auto_ptr_ref to fascilitate...
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
3
by: ruskie | last post by:
I created a user control with two public properties and drop it to an aspx page as the following <uc1:myUserControl id="myUserControl1" runat="server" MyProperty1="<%= this.MyVar1 %>"...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
15
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
0
by: submicron | last post by:
Hi All, I'm having problems implementing a readonly interface to a set of classes. Heres a simplification of the code: interface IreadonlyBase { // Does gets only on base class } class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.