473,654 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<H Integer> d(new HInteger(5));
line 39: std::auto_ptr<H Object> 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<H Object>::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<B ase> a(new Base());
std::auto_ptr<D erived> b(new Derived());
std::auto_ptr<B ase> 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 4570

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<H Integer> d(new HInteger(5));
line 39: std::auto_ptr<H Object> 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<H Object>::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<H Object>, 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<H Integer> d(new HInteger(5));
line 39: std::auto_ptr<H Object> 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<H Object>::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<B ase> a(new Base());
std::auto_ptr<D erived> b(new Derived());
std::auto_ptr<B ase> 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
2097
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 conversion, according to my compiler. Seems there are too many constructors and operator conversions. But this code works:
4
3791
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 returning from a functoin and managed to get that working. However, when I try to assign a value returned from this function, it doesn't work. I looked at the auto_ptr<> implementation and don't see clearly how it is handling it either. Anyways...
23
2645
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 ownership expectation on auto_pointer.
4
52969
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>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
1
1656
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 suppressed in the grid. How can I resolve this ?
3
1501
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 %>" MyProperty2="<%= this.MyVar2 %>" > </uc1:myUserControl>
10
2617
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 section 4.2. The copy constructor is defined as: auto_ptr (auto_ptr& rhs) throw() : ap (rhs. release()) { }
15
2815
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 return (Base*->type_fild >= Derived_typeid)? Base*: 0;
0
1308
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 baseclass : IreadonlyBase
0
8710
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.