473,657 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Friend Functions and Scope

I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope
What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
Also, the second line is compiling fine - without any problems. In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?

Nov 9 '05 #1
9 2526
Neelesh wrote:
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
> Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope


What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
No.

Xform *x;

is also illegal. The name Xfrom does not exist here.
Also, the second line is compiling fine - without any problems.
Well it shouldn't. It's ill-formed.
In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?


Dunno. What compiler are you using? What's the exact code you are
compiling?
Jonathan

Nov 9 '05 #2

"Neelesh" <ne***********@ gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
> Like a member function, a friend declaration doesnot introduce a name
into an enclosing space.
True.
For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope



What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
You are right about the fact that the class's size is unknown.

However, consider this:
class Matrix {
friend class XForm;
} ;

XForm* x; // error, no Xform in scope

Here, the class's size is *not* required to be known. However there will be
a compile time error saying something to the effect of "unknown identifier"
since there is no XForm in scope.
Also, the second line is compiling fine - without any problems. In


This is the error I get with the same code you posted:

"test.cpp", line 9: error: identifier "invert" is undefined
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>

Nov 9 '05 #3

Jonathan Mcdougall wrote:
Dunno. What compiler are you using? What's the exact code you are
compiling?


The exact code (word to word, copied and pasted from (DevC++) is

/********** start *************** **********/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

int main()
{
return 0;
}

/********* end *************** ************/

The compilers used are g++(3.4.2) and VC++6.0.
For g++, options used are -Wall -Werror -pedantic -ansi
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.

Nov 9 '05 #4

Neelesh wrote:
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.


Oh, I just forgot to mention one (obvious) point that I am talking
only about the "compiler errors". It is clear that there will be linker
errors since the function is not defined. But that is not an issue.

Nov 9 '05 #5
Neelesh wrote:
Jonathan Mcdougall wrote:
Dunno. What compiler are you using? What's the exact code you are
compiling?
The exact code (word to word, copied and pasted from (DevC++) is

/********** start *************** **********/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope

int main()
{
return 0;
}

/********* end *************** ************/

The compilers used are g++(3.4.2) and VC++6.0.


This was found as a bug in g++, it was deprecated in 3.4, but I don't
know the current status.
For g++, options used are -Wall -Werror -pedantic -ansi
In both cases the code is compiling properly without a single warning
or error.
Also tried with DevC++ (I guess its quite an old version, 4.9.8.0) and
it gives a warning that invert is not defined - but there is no error.


You are working with old compilers. This syntax was accepted before the
standard so old compilers (<1998) are not aware of the changes.

You can be assured that the given program is ill-formed. In doubt,
always check at http://www.comeaucomputing.com/tryitout/.
Jonathan

Nov 9 '05 #6
I think "declaratio n of a friend function " is not a declaration of the
function.
You declear friend funciton just for the class Matrix . but when you refers
to it, the compiler should find a declaration of the function.

"Neelesh" <ne***********@ gmail.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.com...
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
> Like a member function, a friend declaration doesnot introduce a name
into an enclosing space. For example :

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;

//Xform x; // error, no Xform in scope
Matrix (*p) (const Matrix&) = & invert; // error, no invert() in scope


What I observe is a bit different - it is fine that there is an error
for Xform, but the reason is not that Xform is not in scope - the
reason is that the class's size is not known.
Also, the second line is compiling fine - without any problems. In
other words, I find that the declaration of a friend function or a
class inside a class _does_ bring the name into the scope.

Can somebody explain where I am going wrong?

Nov 9 '05 #7
> This was found as a bug in g++, it was deprecated in 3.4, but I don't
know the current status.

You can be assured that the given program is ill-formed. In doubt,
always check at http://www.comeaucomputing.com/tryitout/.


That solves my doubt. Thanks a lot.
BTW, I tested it with g++ 4.0.0 and the bug still exists.
The code is also compilable on HP-UX's native compiler (aCC)
However, VisualAge 6.0 for AIX gives a compiler error saying that
invert is undefined.

Nov 9 '05 #8

"Neelesh" <ne***********@ gmail.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...

/********** start *************** **********/

class Matrix {
friend class XForm;
friend Matrix invert(const Matrix&);
// ...
} ;
//Xform x; // error, no Xform in scope


By the way, did you realize that "Xform" above is different from "XForm"
earlier (note the upper case "F"). Not that it matters in this case since
Xform and XForm should cause the compiler to complain.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Nov 9 '05 #9

Sumit Rajan wrote:
By the way, did you realize that "Xform" above is different from "XForm"
earlier (note the upper case "F"). Not that it matters in this case since
Xform and XForm should cause the compiler to complain.

Ahh! Thanks for pointing that out. It didnot matter becuase that line
was commented. But yes, a valid point.

Nov 9 '05 #10

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

Similar topics

10
2303
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const {
15
6578
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
1
2233
by: timbobd | last post by:
I have encountered a situation that I don't understand. When I call a sub of Friend scope (in an object with Friend scope), I am getting an error "Public member 'subname' not found in type 'myType'". Any idea why? Since Friend scope should be visible within my assembly, and everying is taking place within a single project, I don't understand why this error is occuring. Here is some illustrative code, where I would get the error in the...
5
1819
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean? "laborated-type-specifiers and friend declarations may introduce a
3
1732
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
5
1392
by: Jameson | last post by:
Hi, I have a property defined something like: Public Property Index As Integer Get Return m_Index End Get
3
2472
by: Rahul | last post by:
Hi Everyone, The following code works fine, class A { private: friend void sample(A& obj) { printf("in friend...\n"); printf("%d",obj.i);
6
3162
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend void func()
21
4699
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8392
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
8305
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,...
0
8823
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...
1
8503
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
7321
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...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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();...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.