Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Friend Functions and Scope

Reply
Thread Tools

Friend Functions and Scope

 
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005
I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
where it is mentioned that
Quote:
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?

 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      11-09-2005
Neelesh wrote:
> I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
> where it is mentioned that
>
Quote:
> 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

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      11-09-2005

"Neelesh" <> wrote in message
news: ups.com...
>I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
> where it is mentioned that
>
Quote:
> Like a member function, a friend declaration doesnot introduce a name
> into an enclosing space.
Quote:

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 <>



 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005

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.

 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005

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.

 
Reply With Quote
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      11-09-2005
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 (<199 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

 
Reply With Quote
 
shikn
Guest
Posts: n/a
 
      11-09-2005
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" <> wrote in message
news: ups.com...
> I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
> where it is mentioned that
>
Quote:
> 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?
>



 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005
> 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.

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      11-09-2005

"Neelesh" <> wrote in message
news: 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 <>


 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      11-09-2005

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Must See Deifferences between Friend & Best Friend wonderfulinfo@gmail.com Digital Photography 17 08-07-2006 01:34 PM
Friend brings a friend. Shisha Girl MCSE 4 03-03-2006 02:42 AM
Friend brings a friend. Shisha Girl C++ 1 03-02-2006 01:23 PM
Friend brings a friend. Shisha Girl C Programming 0 03-02-2006 02:36 AM
Friend brings a friend. Shisha Girl Python 0 03-02-2006 02:28 AM



Advertisments