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

Discussion in 'C++' started by Stephan Hoffmann, Dec 11, 2005.

  1. 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
     
    Stephan Hoffmann, Dec 11, 2005
    #1
    1. Advertising

  2. 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>::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.
     
    Neelesh Bodas, Dec 11, 2005
    #2
    1. Advertising

  3. Resolved: problem with auto_ptr<> and assigning derived class to base class object

    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>::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
     
    Stephan Hoffmann, Dec 11, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    597
    justanotherguy
    Dec 3, 2004
  2. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,586
    Dave Rahardja
    Jan 11, 2005
  3. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
  5. , India
    Replies:
    1
    Views:
    700
    Pascal J. Bourguignon
    Apr 22, 2008
Loading...

Share This Page