473,404 Members | 2,170 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,404 software developers and data experts.

confusion about friend declarator

the following is an example of code that won't compile.
(I've reproduced the problem in a simplistic format )

....
#include <iostream>
using namespace std;

class MyClass
{
public:
unsigned int myint;

ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

};

I get : error C2804: binary 'operator <<' has too many parameters

However I can fix this if I declare it as

friend ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

Why? The function is doing nothing that requires access to the internals
of the class ( there are no internals !! )

Jul 23 '05 #1
2 2452
Mark wrote:
the following is an example of code that won't compile.
(I've reproduced the problem in a simplistic format )

...
#include <iostream>
using namespace std;

class MyClass
{
public:
unsigned int myint;

ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

};

I get : error C2804: binary 'operator <<' has too many parameters

However I can fix this if I declare it as

friend ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

Why? The function is doing nothing that requires access to the
internals of the class ( there are no internals !! )


The sedond declaration declares a non-member function in the enclosing scope.

Jonathan
Jul 23 '05 #2
Mark wrote:
the following is an example of code that won't compile.
(I've reproduced the problem in a simplistic format )

...
#include <iostream>
using namespace std;

class MyClass
{
public:
unsigned int myint;

ostream& operator<<( ostream& o,MyClass& myclass)
Here you define it as member operator with two parameters. But the member
operator must only have one. It takes the this-object as left-hand
argument.
{
return o;
}

};

I get : error C2804: binary 'operator <<' has too many parameters

However I can fix this if I declare it as

friend ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

Why ?
Now it's a non-member operator, which needs two parameters. That one is just
a shorter way to write:

class MyClass
{
//...
friend ostream& operator<<( ostream& o,MyClass& myclass);
};

ostream& operator<<( ostream& o,MyClass& myclass)
{
return o;
}

The function is doing nothing that requires access to the internals
of the class ( there are no internals !! )


For the declaration of a function, it doesn't matter what that function
does.

Jul 23 '05 #3

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

Similar topics

0
by: Gil | last post by:
Hope this is the right group to post to. I compiled C++ code that ran fine on Windows with VisualStudio 6.0. I then moved the code to Solaris 9 and compiled with g++, but am getting the...
7
by: xxx | last post by:
I am having difficulty seeing why cv_qualifier, which is used as a type specifier in the declaration section of the ISO/IEC grammar, is defined in the declarator section. A declaration can consist...
0
by: Alex Sedow | last post by:
Standart describe grammar for events as (in EBNF): event-declaration: "event" type variable-declarators ";" "event" type member-name "{" event-accessor-declarations "}" ...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
7
by: anjogasa | last post by:
In the midst of reading "The C++ Programming Language", the latest edition by Stroustrup, and I find myself butting my head against a few paragraphs. I have searched the errata on the website, and...
5
by: Remco van Engelen | last post by:
Hello, I have a question regarding the ISO C grammar. The syntax of a direct-declarator reads (section A.2.2, page 413 in my copy; the (R1) is just to 'name' the rule for later reference): ...
5
by: Jameson | last post by:
Hi, I have a property defined something like: Public Property Index As Integer Get Return m_Index End Get
10
by: mdh | last post by:
Sorry in advance if this is really dumb, but I am trying to get my head around exactly what the declarator is. In the FAQ, 1.21, part of it says "C declarations ....come in 2 parts, a base type and...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
tracyyun
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...
0
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,...

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.