473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

converting from base class to derived class

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
5 1498
"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

"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
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
"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a...
14
by: Sridhar R | last post by:
Consider the code below, class Base(object): pass class Derived(object): def __new__(cls, *args, **kwds): # some_factory returns an instance of Base # and I have to derive from this...
1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
3
by: DDE | last post by:
Hi all, I have defined a meththod supposed to do some treatment with the class it receives as argument. This method can receive different type of classes so it's argument is defined as an...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
by: Dennis Jones | last post by:
Hello, I have a couple of classes that look something like this: class RecordBase { }; class RecordDerived : public RecordBase {
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
6
by: jnonly | last post by:
Hello, I recently converted projects from VS7.1 (2003) to VS9.0 (2008) and am getting the following error on some subroutine declarations: error BC30284: sub 'SomeSub' cannot be declared...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.