converting from base class to derived class

  • Thread starter Charles Jamieson
  • Start date
C

Charles Jamieson

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
 
D

David Hilsee

Charles Jamieson said:
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.
 
A

Aguilar, James

Charles Jamieson said:
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?
 
C

Charles Jamieson

David said:
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
 
D

David Hilsee

Aguilar said:
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.
 
A

Aguilar, James

David Hilsee said:
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.

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top