converting from base class to derived class

Discussion in 'C++' started by Charles Jamieson, Aug 8, 2004.

  1. I have two classes, a base class, CBaseClass, and its derived class,
    CDerivedClass.

    I overload the insertion operator as

    ostream& operator << ( ostream&, CBaseClass& );

    Then I define an object as

    CDerivedClass object;

    I then have the line

    std::cout << object;

    When I compile and link this code (using gcc 3.3.3), I get the following
    error message

    undefined reference to `operator<<(std::basic_ostream<char,
    std::char_traits<char> >&, CDerivedClass&)'

    How can I get the compiler to use the overloaded insertion operator as I
    defined it?

    -charles
    Charles Jamieson, Aug 8, 2004
    #1
    1. Advertising

  2. Charles Jamieson

    David Hilsee Guest

    "Charles Jamieson" <> wrote in message
    news:tcvRc.250714$JR4.79816@attbi_s54...
    > I have two classes, a base class, CBaseClass, and its derived class,
    > CDerivedClass.
    >
    > I overload the insertion operator as
    >
    > ostream& operator << ( ostream&, CBaseClass& );
    >
    > Then I define an object as
    >
    > CDerivedClass object;
    >
    > I then have the line
    >
    > std::cout << object;
    >
    > When I compile and link this code (using gcc 3.3.3), I get the following
    > error message
    >
    > undefined reference to `operator<<(std::basic_ostream<char,
    > std::char_traits<char> >&, CDerivedClass&)'
    >
    > How can I get the compiler to use the overloaded insertion operator as I
    > defined it?


    The code looks fine, aside from the nitpick that operator<< might take a
    const CBaseClass& instead of a CBaseClass&. The error message makes me
    think that there's something you didn't post that's causing the problem,
    like a declaration of operator<<(std::basic_ostream<char,
    std::char_traits<char> >&, CDerivedClass&) that lacks a definition. It
    might be helpful if you post a complete example that causes g++ to emit the
    same error message.

    --
    David Hilsee
    David Hilsee, Aug 8, 2004
    #2
    1. Advertising

  3. "Charles Jamieson" <> wrote in message
    news:tcvRc.250714$JR4.79816@attbi_s54...
    > I have two classes, a base class, CBaseClass, and its derived class,
    > CDerivedClass.
    >
    > I overload the insertion operator as
    >
    > ostream& operator << ( ostream&, CBaseClass& );


    Shouldn't this be virtual if you want it to work for subclasses?
    Aguilar, James, Aug 8, 2004
    #3
  4. David Hilsee wrote:
    > "Charles Jamieson" <> wrote in message
    > news:tcvRc.250714$JR4.79816@attbi_s54...
    >
    >>I have two classes, a base class, CBaseClass, and its derived class,
    >>CDerivedClass.
    >>
    >>I overload the insertion operator as
    >>
    >> ostream& operator << ( ostream&, CBaseClass& );
    >>
    >>Then I define an object as
    >>
    >> CDerivedClass object;
    >>
    >>I then have the line
    >>
    >> std::cout << object;
    >>
    >>When I compile and link this code (using gcc 3.3.3), I get the following
    >>error message
    >>
    >> undefined reference to `operator<<(std::basic_ostream<char,
    >>std::char_traits<char> >&, CDerivedClass&)'
    >>
    >>How can I get the compiler to use the overloaded insertion operator as I
    >>defined it?

    >
    >
    > The code looks fine, aside from the nitpick that operator<< might take a
    > const CBaseClass& instead of a CBaseClass&. The error message makes me
    > think that there's something you didn't post that's causing the problem,
    > like a declaration of operator<<(std::basic_ostream<char,
    > std::char_traits<char> >&, CDerivedClass&) that lacks a definition. It
    > might be helpful if you post a complete example that causes g++ to emit the
    > same error message.
    >

    David,

    You were right, I did have another declaration for the derived class
    without a definition. Thanks.

    -charles
    Charles Jamieson, Aug 8, 2004
    #4
  5. Charles Jamieson

    David Hilsee Guest

    "Aguilar, James" <> wrote in message
    news:cf64e5$sff$...
    >
    > "Charles Jamieson" <> wrote in message
    > news:tcvRc.250714$JR4.79816@attbi_s54...
    > > I have two classes, a base class, CBaseClass, and its derived class,
    > > CDerivedClass.
    > >
    > > I overload the insertion operator as
    > >
    > > ostream& operator << ( ostream&, CBaseClass& );

    >
    > Shouldn't this be virtual if you want it to work for subclasses?


    It's not a member function, so it can't be made virtual. However, it could
    delegate to a virtual member function (e.g. "virtual ostream&
    Print(std::eek:stream&) const;") in CBaseClass if Charles wanted to invoke
    functionality provided by subclasses.

    --
    David Hilsee
    David Hilsee, Aug 8, 2004
    #5
  6. "David Hilsee" <> wrote in message
    news:...
    > "Aguilar, James" <> wrote in message
    > news:cf64e5$sff$...
    > >
    > > "Charles Jamieson" <> wrote in message
    > > news:tcvRc.250714$JR4.79816@attbi_s54...
    > > > I have two classes, a base class, CBaseClass, and its derived class,
    > > > CDerivedClass.
    > > >
    > > > I overload the insertion operator as
    > > >
    > > > ostream& operator << ( ostream&, CBaseClass& );

    > >
    > > Shouldn't this be virtual if you want it to work for subclasses?

    >
    > It's not a member function, so it can't be made virtual. However, it

    could
    > delegate to a virtual member function (e.g. "virtual ostream&
    > Print(std::eek:stream&) const;") in CBaseClass if Charles wanted to invoke
    > functionality provided by subclasses.
    >
    > --
    > David Hilsee


    Oooh, yeah. I'm a noob, please forgive. I forgot that if it's gonna be a
    member function, the object of such and so a type has to be on the left
    side.
    Aguilar, James, Aug 9, 2004
    #6
    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. Sridhar R
    Replies:
    14
    Views:
    1,381
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  2. Replies:
    1
    Views:
    383
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    371
    Victor Bazarov
    May 23, 2007
  4. Replies:
    2
    Views:
    692
  5. , India
    Replies:
    8
    Views:
    932
    gwowen
    Aug 18, 2010
Loading...

Share This Page