Hi, I'm having some trouble overloading the << operator. I have the
following, very simple code:
#include <iostream>
using namespace std;
class test
{
private:
int val;
public:
test():val(0){}
const int GetVal() const{
return val;
}
ostream& operator<< (ostream& os , test& a) {
os << a.GetVal();
return os;
}
};
//------End Sample Code
I get the following error during compile. What does this error mean?
error: 'std::ostream& test::operator< <(std::ostream& , test&)' must
take exactly one argument
I want to do something like
test MyTest;
cout << test << endl;
Thanks 8 8379
jois.de.vivre wrote: Hi, I'm having some trouble overloading the << operator. I have the following, very simple code:
#include <iostream> using namespace std;
class test { private: int val; public: test():val(0){} const int GetVal() const{ return val; }
ostream& operator<< (ostream& os , test& a) { os << a.GetVal(); return os; } }; //------End Sample Code
I get the following error during compile. What does this error mean?
error: 'std::ostream& test::operator< <(std::ostream& , test&)' must take exactly one argument
operator<< can either be a member function and take one argument - an
ostream& - or it can be a nonmember function and take two arguments -
an ostream& and a const test&. You conflate the two - you both make it
a member function, but you also include two arguments. Hence the error
message. Choose one or the other. And typically people make it a
nonmember function, and I suggest you do the same.
By the way, make it const test&, not test&.
Best regards,
Tom jo***********@g mail.com wrote: ostream& operator<< (ostream& os , test& a) { os << a.GetVal(); return os; } }; //------End Sample Code
I get the following error during compile. What does this error mean?
error: 'std::ostream& test::operator< <(std::ostream& , test&)' must take exactly one argument
It means the member operator<< must have one argument.
try
friend std::ostream& operator<<( std::ostream& os, const test& a)
{
os << a.GetVal();
return os;
}
Or if no private members are accessed, put the operator outside of the
class.
Ian
"Thomas Tutone" <Th***********@ yahoo.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. . jois.de.vivre wrote:
Hi, I'm having some trouble overloading the << operator. I have the following, very simple code:
#include <iostream> using namespace std;
class test { private: int val; public: test():val(0){} const int GetVal() const{ return val; }
ostream& operator<< (ostream& os , test& a) { os << a.GetVal(); return os; } }; //------End Sample Code
I get the following error during compile. What does this error mean?
error: 'std::ostream& test::operator< <(std::ostream& , test&)' must take exactly one argument
operator<< can either be a member function and take one argument - an ostream& - or it can be a nonmember function and take two arguments - an ostream& and a const test&. You conflate the two - you both make it a member function, but you also include two arguments. Hence the error message. Choose one or the other. And typically people make it a nonmember function, and I suggest you do the same.
Well, OP is probably trying to use it in this usual context:
test my_test;
cout << my_test;
In that case, operator<< must be a free function and take two arguments.
Also, regarding Ian's post, if needed, the 'friend' keyword goes with the
in-class declaration of the free function; not with the definition of it:
class test
{
friend ostream & operator<< (ostream &, test const &);
/* ... */
};
ostream & operator<< (ostream & os, test const & object)
{
return os << object.val;
}
Of course, since private access is not needed in the OP's case, it is better
to just define the free function:
class test
{
/* ... */
};
ostream & operator<< (ostream & os, test const & object)
{
return os << object.GetVal() ;
}
Ali
Thomas Tutone <Th***********@ yahoo.com> wrote: operator<< can either be a member function and take one argument - an ostream& - or it can be a nonmember function and take two arguments - an ostream& and a const test&. You conflate the two - you both make it a member function, but you also include two arguments. Hence the error message. Choose one or the other. And typically people make it a nonmember function, and I suggest you do the same.
I looked in the FAQ and did not find the answer to this question:
Why are the nonmember function versions preferred over the member
function versions?
--
Marcus Kwok
Marcus Kwok wrote: Thomas Tutone <Th***********@ yahoo.com> wrote:
operator<< can either be a member function and take one argument - an ostream& - or it can be a nonmember function and take two arguments - an ostream& and a const test&. You conflate the two - you both make it a member function, but you also include two arguments. Hence the error message. Choose one or the other. And typically people make it a nonmember function, and I suggest you do the same.
I looked in the FAQ and did not find the answer to this question:
Why are the nonmember function versions preferred over the member function versions?
If you are trying to output _your_ class to an ostream object using
operator << (insertion into stream), then you simply *can't* have your
overloaded operator as a member because it has to be a member of the
'ostream', and you're not allowed to modify it. You're stuck with
implementing operator<< as non-member.
Now, as to why sometimes implementing two-operand operators is better
as non-member than a member, it's covered in Effective C++ book. And
you can only consider the reasons if you have a choice. In your case
you don't.
V
Victor Bazarov <v.********@com acast.net> wrote: Marcus Kwok wrote: Thomas Tutone <Th***********@ yahoo.com> wrote:
operator<< can either be a member function and take one argument - an ostream& - or it can be a nonmember function and take two arguments - an ostream& and a const test&. You conflate the two - you both make it a member function, but you also include two arguments. Hence the error message. Choose one or the other. And typically people make it a nonmember function, and I suggest you do the same.
I looked in the FAQ and did not find the answer to this question:
Why are the nonmember function versions preferred over the member function versions?
If you are trying to output _your_ class to an ostream object using operator << (insertion into stream), then you simply *can't* have your overloaded operator as a member because it has to be a member of the 'ostream', and you're not allowed to modify it. You're stuck with implementing operator<< as non-member.
Doesn't this contradict Thomas's first sentence above? What about the
member function
ostream& MyClass::operat or<<(ostream&);
Will this not allow you to do, for example,
MyClass c;
std::cout << c;
or does the declaration imply
c << std::cout;
(which is backwards from the way it's supposed to be)?
Now, as to why sometimes implementing two-operand operators is better as non-member than a member, it's covered in Effective C++ book. And you can only consider the reasons if you have a choice. In your case you don't.
Thanks, I'll look into picking up that book; it seems to be very well
regarded.
--
Marcus Kwok
Marcus Kwok wrote: Victor Bazarov <v.********@com acast.net> wrote:
Marcus Kwok wrote:
Thomas Tutone <Th***********@ yahoo.com> wrote:
operator< < can either be a member function and take one argument - an ostream& - or it can be a nonmember function and take two arguments - an ostream& and a const test&. You conflate the two - you both make it a member function, but you also include two arguments. Hence the error message. Choose one or the other. And typically people make it a nonmember function, and I suggest you do the same.
I looked in the FAQ and did not find the answer to this question:
Why are the nonmember function versions preferred over the member function versions? If you are trying to output _your_ class to an ostream object using operator << (insertion into stream), then you simply *can't* have your overloaded operator as a member because it has to be a member of the 'ostream', and you're not allowed to modify it. You're stuck with implementin g operator<< as non-member.
Doesn't this contradict Thomas's first sentence above? What about the member function
ostream& MyClass::operat or<<(ostream&);
Will this not allow you to do, for example,
MyClass c; std::cout << c;
No. It would allow you to do this
MyClass c;
c << std::cout;
or does the declaration imply
c << std::cout; (which is backwards from the way it's supposed to be)?
Yes. Well, it's not backwards. The left operand of a binary member is
always the object of the class in which the operand is the member. You
could, for fun, define it as
ostream& MyClass::operat or>>(ostream&);
and use it as
MyClass c;
c >> std::cout << std::endl;
A bit awkward, but some may find it making sense...
V
Victor Bazarov <v.********@com acast.net> wrote: Marcus Kwok wrote: ostream& MyClass::operat or<<(ostream&);
Will this not allow you to do, for example,
MyClass c; std::cout << c; No. It would allow you to do this
MyClass c; c << std::cout;
or does the declaration imply
c << std::cout; (which is backwards from the way it's supposed to be)?
Yes. Well, it's not backwards. The left operand of a binary member is always the object of the class in which the operand is the member.
OK, I understand. Thanks.
You could, for fun, define it as
ostream& MyClass::operat or>>(ostream&);
and use it as
MyClass c; c >> std::cout << std::endl;
A bit awkward, but some may find it making sense...
Maybe it will be useful when they start an Obfuscated C++ contest
(IOC++CC?) :)
--
Marcus Kwok This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: gipsy boy |
last post by:
I made a Stack structure, as an exercise on templates.
Stack ll;
Is this used irl? : "ll << o" to push an object pointer to the end of
the list and "ll >> &o" to push it off and save the address in &o or
s'thing similar. I mean the usage of the <</>> operators..
We need to learn good style, I was wodnering if this is..good style..or
just a newbie's obsession with operator overloading.
--
- gipsy boy
|
by: pmatos |
last post by:
Hi all,
I have the following code:
class test {
public:
test(const std::string *n) : name(n) {}
virtual ~test() {}
const std::string * getName() { return name; }
|
by: pmatos |
last post by:
Hi all,
I'm overloading operator<< for a lot of classes. The question is about
style. I define in each class header the prototype of the overloading
as a friend. Now, where should I define the overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
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,
|
by: spibou |
last post by:
I saw it at http://www.faqs.org/rfcs/rfc1305.html
Is it not the same as writing ``if (m)'' ?
| |
by: iLL |
last post by:
Okay, I’m just a little confused on exactly what the system is doing when I say:
#include <iostream>
class test
{
private:
int i;
public:
|
by: Sunny |
last post by:
The way to overload operator << is : ostream& operator << (ostream&
os, const Obj& obj);
and this is a member function.
My question is why do we need to provide a const reference of Obj as
argument when it is a member function and its members accessible
through this pointer ?
Isnt cout << obj;
equivalent to obj.operator<<(cout) ?
Thanks
|
by: buburuz |
last post by:
Hi,
I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an overloaded operator<<, which takes a string as input parameter and returns a reference to an ostream object (std::cout in this case).
In the main file I instantiate an object of MyOut and then write a message to console in two different ways:
1....
|
by: Rob McDonald |
last post by:
I would like to force all the classes in my hierarchy to implement the
<< operator for testing purposes. My base class is a pure virtual
class.
I started out by working with operator overloading in the derived
class. I have been trying to use what I learned there to create an
appropriate virtual class to force overloading.
class Base{
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 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
| |