By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,501 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,501 IT Pros & Developers. It's quick & easy.

converting from base class to derived class

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Charles Jamieson" <cj*******@no.junk> 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
Jul 22 '05 #2

P: n/a

"Charles Jamieson" <cj*******@no.junk> 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?
Jul 22 '05 #3

P: n/a
David Hilsee wrote:
"Charles Jamieson" <cj*******@no.junk> 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

Jul 22 '05 #4

P: n/a
"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...

"Charles Jamieson" <cj*******@no.junk> 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::ostream&) const;") in CBaseClass if Charles wanted to invoke
functionality provided by subclasses.

--
David Hilsee
Jul 22 '05 #5

P: n/a

"David Hilsee" <da*************@yahoo.com> wrote in message
news:CN********************@comcast.com...
"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...

"Charles Jamieson" <cj*******@no.junk> 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::ostream&) 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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.