473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create 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 2510
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*********************@g47g2000cwa.googlegro ups.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.hcltech.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 "declaration 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*********************@g47g2000cwa.googlegro ups.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*********************@g44g2000cwa.googlegro ups.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.hcltech.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
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
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...
1
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...
5
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?...
3
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
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
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
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...
21
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...
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...
1
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.