473,320 Members | 1,914 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,320 software developers and data experts.

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.GetMyClass();

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

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 1688
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.GetMyClass();

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

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(const 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.GetMyClass();

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::operator<<(std::ostream&, BADLIB::BadClass_t&)«:
// main.c++:110: Error: no match for »operator<<« in »aStream <<
BADLIB::BadClass_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(false);
aStream << "Begin Opening (" << aWC.GetValue() << ") End Opening
\n";
}
else {
aWC.SetMode(true);
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(const GoodClass_t&);
GoodClass_t GetValue() const;
private:
GoodClass_t itsGoodObj;
};
ostream& operator <<(ostream&, /* const */ BadClass_t&);
}

BADLIB::
BadClass_t::
BadClass_t(const GoodClass_t& aGoodObj)
: itsGoodObj(aGoodObj) {

}

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(aGoodObj);

// 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::operator<<(std::ostream&, BADLIB::BadClass_t&)«:
// main.c++:110: Error: no match for »operator<<« in »aStream <<
BADLIB::BadClass_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(false);
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(true);
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(const 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(false);

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(false);
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
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...
16
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...
3
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
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...
7
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...
5
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...
25
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...
5
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
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,...
8
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,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.