473,770 Members | 6,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question regarding operator << overloading

Hi all,

I have a question regarding operator <<.

A lib of mine contains a class with an overloaded operator << as NON-
class member. This would look like:
#include <iostream>
#include <libsomelib.h >

using namespace std;
using namespace SOMELIB;

int main() {

myClass_t myObject;

cout << myObject;

return 0;
}
The example above works fine. But now I'm building a second lib with
newClass_t and myClass_t as private member:
#include <ostream>
#include <libsomelib.h >

class newClass_t {

public:
...
myClass_t GetMyClass() const;
private:
myClass_t itsMyClass;
}

&ostream operator <<(ostream&, const newClass_t&)
The source file:
...

ostream& <<(ostream& aStream, const newClass_t& aNewClass) {

aStream << aNewClass.GetMy Class();

return aStream;
}
OK, now there is a problem. It's the line "aStream <<
aNewClass.GetMy Class();". It does not compile. The error: no match for
»operator<<« in »aStream << newClass_t::Get MyClass()

So the compiler can't find the overloaded operator. Why? It's inside
libsomelib.h. What's wrong?

Thanks for answering
Jun 27 '08 #1
8 1705
Goran wrote:
Hi all,

I have a question regarding operator <<.

A lib of mine contains a class with an overloaded operator << as NON-
class member. This would look like:
#include <iostream>
#include <libsomelib.h >

using namespace std;
using namespace SOMELIB;

int main() {

myClass_t myObject;

cout << myObject;

return 0;
}
The example above works fine.
You didn't say how your operator<< is declared. It might matter.
But now I'm building a second lib with
newClass_t and myClass_t as private member:
#include <ostream>
#include <libsomelib.h >

class newClass_t {

public:
...
myClass_t GetMyClass() const;
private:
myClass_t itsMyClass;
}

&ostream operator <<(ostream&, const newClass_t&)
The source file:
...

ostream& <<(ostream& aStream, const newClass_t& aNewClass) {

aStream << aNewClass.GetMy Class();

return aStream;
}
OK, now there is a problem. It's the line "aStream <<
aNewClass.GetMy Class();". It does not compile. The error: no match for
»operator<<« in »aStream << newClass_t::Get MyClass()

So the compiler can't find the overloaded operator. Why? It's inside
libsomelib.h. What's wrong?
Namespaces? Did you declare your operator<< in your namespace?

Read the FAQ 5.8 please and follow the suggestions.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
[...]
You didn't say how your operator<< is declared. It might matter.
[...]
You're right. Here is a new example:

This main() works...

#include ...

using namespace std;
using namespace SOMELIB;

int main() {

myClass_t myObject;

cout << myObject;

return 0;

}

Because next lib works too:

using namespace std;

namespace SOMELIB {

class myClass_t {
...
};
ostream& operator <<(ostream&, const myClass_t&);
}
Now I want to build a new lib...

#include <ostream>
#include <libsomelib.h >

using namespace std;
using namespace SOMELIB;

namespace NEWLIB {

class newClass_t {

public:
...
void AddMyClass(cons t myClass_t&);
myClass_t GetMyClass() const;
private:
myClass_t itsMyClass;
};
ostream& operator <<(ostream&, const newClass_t&);
}

newClass_t works also fine but one problem! If I use the operator <<
from myClass_t inside the operator << definition from newClass_t I
got the error. The definition looks like:

ostream&
NEWLIB::
operator <<(ostream& aStream, const newClass_t& aNewClass) {

// Here comes the error!
aStream << aNewClass.GetMy Class();

return aStream;
}
Jun 27 '08 #3
Goran wrote:
>[...]
You didn't say how your operator<< is declared. It might matter.
[...]

You're right. Here is a new example:

This main() works...

#include ...

[..]
I am sorry. I probably wasn't clear enough. The information you've
provided is good, but I just don't have time to fill in all the "..."
that you have left in there before I can run it through my compiler,
sorry. And without running it through the compiler I can't even confirm
that the error is the one you're asking about.

Please don't get me wrong, but unless you invest some of *your* time
reducing your code to the bare minimum yet still exhibiting the problem
you're describing (do see the FAQ 5.8 please), how can you expect us to
invest *our* time to help you, really? Start by creating a new file (or
files) that should represent your project in miniature. You can copy
the code from your existing project, I am sure of it. Then remove all
the stuff that is not relevant. Then make sure it all compiles _except_
for the problem you're experiencing. If by then you haven't figured out
what is wrong, post that new, stripped down, code here and ask your
question again. You have to understand that only you have the advantage
of being able to take the non-working code and compile it. Share the
advantage with the newsgroup and the newsgroup will happily share their
knowledge with you. Fair?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
// You are right! I'm a lazy dude :D. Here's a correct example which
does not
// compile! The error:
// main.c++: In function »std::ostream&
BADLIB::operato r<<(std::ostrea m&, BADLIB::BadClas s_t&)«:
// main.c++:110: Error: no match for »operator<<« in »aStream <<
BADLIB::BadClas s_t::GetValue() const()«

#include <iostream>
#include <string>
#include <ostream>


//// START GOOD CLASS ////

// The GoodClass_t is nothing special but the overloaded operator <<.
// The operator calls the method SetMode and GetMode to change the
output.
using namespace std;

namespace GOODLIB {

class GoodClass_t {

public:
GoodClass_t();
string GetValue() const;
void SetMode(const bool&);
bool GetMode() const;
private:
string itsValue;
bool itsMode;
};
// this operator works
ostream& operator <<(ostream&, GoodClass_t&);
}

GOODLIB::
GoodClass_t::
GoodClass_t() {

itsValue = "foo";
itsMode = true;
}

string
GOODLIB::
GoodClass_t::
GetValue() const {

return itsValue;
}

bool
GOODLIB::
GoodClass_t::
GetMode() const {

return itsMode;
}

void
GOODLIB::
GoodClass_t::
SetMode(const bool& aMode) {

itsMode = aMode;
}

ostream&
GOODLIB::
operator <<(ostream& aStream, GoodClass_t& aWC) {

if(aWC.GetMode( )) {
aWC.SetMode(fal se);
aStream << "Begin Opening (" << aWC.GetValue() << ") End Opening
\n";
}
else {
aWC.SetMode(tru e);
aStream << "Begin Closing (" << aWC.GetValue() << ") End Closing
\n";
}

return aStream;
}
//// END GOOD CLASS ////


//// START BAD CLASS////

// The BadClass_t contains a private GoodClass_t Object available by
// GetValue(). So it's very simple. But in conjunction with
GoodClass_t I
// really don't know where's the failure.
using namespace std;
using namespace GOODLIB;

namespace BADLIB {

class BadClass_t {

public:
BadClass_t(cons t GoodClass_t&);
GoodClass_t GetValue() const;
private:
GoodClass_t itsGoodObj;
};
ostream& operator <<(ostream&, /* const */ BadClass_t&);
}

BADLIB::
BadClass_t::
BadClass_t(cons t GoodClass_t& aGoodObj)
: itsGoodObj(aGoo dObj) {

}

GoodClass_t
BADLIB::
BadClass_t::
GetValue() const {

return itsGoodObj;
}

ostream&
BADLIB::
operator <<(ostream& aStream, /* const */ BadClass_t& aBC) {

aStream << aBC.GetValue() << "\nsome stuff\n" << aBC.GetValue();
}
//// END BAD CLASS////
using namespace std;
using namespace GOODLIB;
using namespace BADLIB;

int main() {

GoodClass_t aGoodObj;
BadClass_t aBadObj(aGoodOb j);

// Begin Opening (foo) End Opening
// Begin Closing (foo) End Closing
// ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^
cout << aGoodObj << aGoodObj; // Works fine

cout << aBadObj; // BAD!!! WHY???

return 0;
}
Jun 27 '08 #5
Goran wrote:
// main.c++: In function »std::ostream&
BADLIB::operato r<<(std::ostrea m&, BADLIB::BadClas s_t&)«:
// main.c++:110: Error: no match for »operator<<« in »aStream <<
BADLIB::BadClas s_t::GetValue() const()«
It would be also good to know which line is 110, don't you think?
[..]
namespace GOODLIB {

class GoodClass_t {

public:
GoodClass_t();
string GetValue() const;
void SetMode(const bool&);
bool GetMode() const;
private:
string itsValue;
bool itsMode;
};
// this operator works
ostream& operator <<(ostream&, GoodClass_t&);
Note that 'GoodClass_t' object is *non-const*. Is there a reason
why this is so?
}

[..]
void
GOODLIB::
GoodClass_t::
SetMode(const bool& aMode) {

itsMode = aMode;
}

ostream&
GOODLIB::
operator <<(ostream& aStream, GoodClass_t& aWC) {

if(aWC.GetMode( )) {
aWC.SetMode(fal se);
So, while *outputting* an object you *set* its mode? I don't know.
Does not seem like a good idea. Anyway...
aStream << "Begin Opening (" << aWC.GetValue() << ") End Opening
\n";
}
else {
aWC.SetMode(tru e);
aStream << "Begin Closing (" << aWC.GetValue() << ") End Closing
\n";
}

return aStream;
}
//// END GOOD CLASS ////


//// START BAD CLASS////

// The BadClass_t contains a private GoodClass_t Object available by
// GetValue(). So it's very simple. But in conjunction with
GoodClass_t I
// really don't know where's the failure.
using namespace std;
using namespace GOODLIB;

namespace BADLIB {

class BadClass_t {

public:
BadClass_t(cons t GoodClass_t&);
GoodClass_t GetValue() const;
So, this class returns an rvalue as well...
private:
GoodClass_t itsGoodObj;
};
ostream& operator <<(ostream&, /* const */ BadClass_t&);
}

[..]
ostream&
BADLIB::
operator <<(ostream& aStream, /* const */ BadClass_t& aBC) {

aStream << aBC.GetValue() << "\nsome stuff\n" << aBC.GetValue();
So, let's examine this. 'aBC.GetValue() ' returns a temporary,
which you try to pass to the op<< that expects an lvalue. The
argument of the op<< is a reference to non-const that cannot be
bound to a temporary.

You need to revise your interface.
}
//// END BAD CLASS////
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #6
First... thanks for your detailed description, Victor!

Regarding your questions:
[...]
It would be also good to know which line is 110, don't you think?
[...]
The Line 110 is:
aStream << aBC.GetValue() << "\nsome stuff\n" << aBC.GetValue();
[...]
ostream& operator <<(ostream&, GoodClass_t&);

Note that 'GoodClass_t' object is *non-const*. Is there a reason
why this is so?
[...]
This is due to the fact that a private member is changed.

[...]
ostream&
GOODLIB::
operator <<(ostream& aStream, GoodClass_t& aWC) {
if(aWC.GetMode( )) {
aWC.SetMode(fal se);

So, while *outputting* an object you *set* its mode? I don't know.
Does not seem like a good idea. Anyway...
[...]
Yeah, because there are two ways of output. E.g. Line 110.

I hope answered all questions. Your analyze below is instructive.
[...]
ostream&
BADLIB::
operator <<(ostream& aStream, /* const */ BadClass_t& aBC) {
aStream << aBC.GetValue() << "\nsome stuff\n" << aBC.GetValue();

So, let's examine this. 'aBC.GetValue() ' returns a temporary,
which you try to pass to the op<< that expects an lvalue. The
argument of the op<< is a reference to non-const that cannot be
bound to a temporary.

You need to revise your interface.
[...]
I will do so.

Greetings, Goran
Jun 27 '08 #7
Goran wrote:
First... thanks for your detailed description, Victor!

Regarding your questions:
[...]
>It would be also good to know which line is 110, don't you think?
[...]
The Line 110 is:
aStream << aBC.GetValue() << "\nsome stuff\n" << aBC.GetValue();
It's too late, isn't it? Just remember to point out certain things you
get for free while in your development environment, to us who can't see
into your monitor from *that afar*.
[...]
>> ostream& operator <<(ostream&, GoodClass_t&);
Note that 'GoodClass_t' object is *non-const*. Is there a reason
why this is so?
[...]
This is due to the fact that a private member is changed.
I get that. The reason I asked the question so you would stop and think
whether you actually need to _change_ that private member during output.
[...]
>>ostream&
GOODLIB::
operator <<(ostream& aStream, GoodClass_t& aWC) {
if(aWC.GetMode( )) {
aWC.SetMode(fal se);
So, while *outputting* an object you *set* its mode? I don't know.
Does not seem like a good idea. Anyway...
[...]
Yeah, because there are two ways of output. E.g. Line 110.
I think what you're saying here is that you need the *alternating* way
of outputting each object. I've never has the need to do that with any
of my objects, but I concede there may be a need. I would, however, in
that case define the 'mode' a *mutable* member of your class and make
'SetMode' a const member and declare the 'aWC' argument a reference to
*const* object.
>>
You need to revise your interface.
[...]
I will do so.
Take a closer look at the 'mutable' suggestion.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #8
Hi!

Victor Bazarov schrieb:
I think what you're saying here is that you need the *alternating* way
of outputting each object. I've never has the need to do that with any
of my objects, but I concede there may be a need. I would, however, in
that case define the 'mode' a *mutable* member of your class and make
'SetMode' a const member and declare the 'aWC' argument a reference to
*const* object.
Or you create two separate output functions (not operator <<) and a
separate function object with external state. I'm afraid keeping the
output mode in the object is not reentrant.
Well, Goran, first try the mutable concept.

Frank
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7213
by: Victor | last post by:
Anyone knows how to write a virtual function for operator<< ? I have a base class and some public derived class from it. For the derived class, I hope they can use << to output their different data. What's more, I want the base class pointers can access the derived ones and then their corresponding operator <<. For example: class base { // virtual and friend can't be put together, so the following // code won't work.
16
3096
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class has two int memebers "dataA", "dataB". The derived class has an additional int member "dataC". I am simply trying to overload the + operator so that 'adding' two objects will sum up the corresponding int members.
3
2035
by: Robert Wierschke | last post by:
Hi I want to overload the operator<< for a class Vector. class Vector { double x; double y; double z;
6
1379
by: Master of C++ | last post by:
Hello Group, Please take a look at the following (simple) C++ code (at the end of this post) that does operator overloading. It gives me the following compile error: complex.cpp: In function `int main()': complex.cpp:59: no match for `cmplx& = cmplx' operator complex.cpp:26: candidates are: cmplx& cmplx::operator=(cmplx&)
7
1831
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class xydata with operator- overloaded: class xydata { public:
5
5101
by: Ian Lazarus | last post by:
Hello, My question is whether it is possible to avoid assignment on the left hand side of an overloaded operator << expression, as in the code below. Without the assignment, the compiler complains. class myclass
25
2856
by: Steve Richter | last post by:
is it possible to overload the << operator in c# similar to the way it is done in c++ ? MyTable table = new MyTable( ) ; MyTableRow row = new MyTableRow( ) ; row << new MyTableCell( "cell1 text contents" ) ; row << new MyTableCell( "cell2 text contents" ) ; table << row ; thanks,
5
1645
by: TOMERDR | last post by:
Hi, I was requested to write a code similar to this: CArchive ar; //Not mfc CArchive..... .... void * vp = &XXX; // any object. ar << vp, sizeof(XXX);
3
3282
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
8
2976
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular, operator =, operator, operator(), and operator-must be nonstatic member function; this ensures that their first operands will be lvalues". I know that these operators must be nonstatic member functions, but why this ensure their first operands will...
0
9592
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
10230
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...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
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
8886
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...
0
5313
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
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.