473,783 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_trait s<char> >&, CDerivedClass&) '

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

-charles

Jul 22 '05 #1
5 1512
"Charles Jamieson" <cj*******@no.j unk> wrote in message
news:tcvRc.2507 14$JR4.79816@at tbi_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_trait s<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_trait s<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.j unk> wrote in message
news:tcvRc.2507 14$JR4.79816@at tbi_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.j unk> wrote in message
news:tcvRc.2507 14$JR4.79816@at tbi_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_tra its<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_trait s<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.NOBOT Swustl.edu> wrote in message
news:cf******** **@newsreader.w ustl.edu...

"Charles Jamieson" <cj*******@no.j unk> wrote in message
news:tcvRc.2507 14$JR4.79816@at tbi_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::ostr eam&) 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******** ************@co mcast.com...
"Aguilar, James" <jf**@cec.NOBOT Swustl.edu> wrote in message
news:cf******** **@newsreader.w ustl.edu...

"Charles Jamieson" <cj*******@no.j unk> wrote in message
news:tcvRc.2507 14$JR4.79816@at tbi_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::ostr eam&) 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
2040
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 Base class (old-style) which defines a default class attribute 'bar' too. I can't modify the source code of this class. Note that Workes does not inherit from Base. - a Derived class which must inherit from Base and is a wrapper around Worker: it...
14
3339
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 instance!
1
4344
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 inherited base member's access level in the derived class: its access level in the base class and the type of inheritance (public, protected, or private). After this determination is made, the following possibilities exist for manually changing the...
3
2404
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 object, see sample beloz public DoSomething(object theClass) { }
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
4
5247
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 attempt to access the object -- since m_object is not actually created and initizialized until after the base constructor has been called. Any thoughts on the practice below? class Base { public:
6
2501
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
2625
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
2413
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 static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
6
2181
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 'Overrides' because it does not override a sub in a base class. The subs on which this occurs have parameters of a type that comes from an external COM library. I have added references to the library
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.