473,583 Members | 3,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2460
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
1297
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 following errors : In file included from corba.h:97, from code_ims_c.hh:13, from code_s.hh:11,
7
2201
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 of specifiers followed by declarators. Because of the cv_qualifier, I am now confused as to what defines a declarator. Can anyone help clear this up...
0
1111
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 "}" variable-declarators: variable-declarator variable-declarators "," variable-declarator
13
2085
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 the confusion......... I thought that the DataView is the view from the dataset, but it seems that the dataview has the records that are in the...
7
7521
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 ----------- 0 1
7
4405
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 since its not in error, my understanding must be. I quote the following: - >From 4.9.1 - The Structure of a Declaration A declaration...
5
3783
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): (R1) direct-declarator: identifier "(" declarator ")"
5
1389
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
1888
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 a declarator......". The example used is char *pc where the declarator " *pc" tells us that " *pc is a character". Can one thus broadly say...
0
7893
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...
0
7821
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...
0
8317
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...
1
7928
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...
0
8188
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 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...
0
5369
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...
0
3839
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
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...

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.