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 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
"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?
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
"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
"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. 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |