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 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
"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?
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
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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!
|
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...
|
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) {
}
|
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
| |
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:
|
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;
|
by: Dennis Jones |
last post by:
Hello,
I have a couple of classes that look something like this:
class RecordBase
{
};
class RecordDerived : public RecordBase
{
|
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...
|
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
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |