By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

Friend Functions and Scope

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a

"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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a

"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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.