Friend Functions and Scope

Discussion in 'C++' started by Neelesh, Nov 9, 2005.

  1. Neelesh

    Neelesh Guest

    I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
    where it is mentioned that
    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?
     
    Neelesh, Nov 9, 2005
    #1
    1. Advertising

  2. Neelesh wrote:
    > I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
    > where it is mentioned that
    >
    >
    > 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
     
    Jonathan Mcdougall, Nov 9, 2005
    #2
    1. Advertising

  3. Neelesh

    Sumit Rajan Guest

    "Neelesh" <> wrote in message
    news:...
    >I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
    > where it is mentioned that
    >

    >




    > 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 <>
     
    Sumit Rajan, Nov 9, 2005
    #3
  4. Neelesh

    Neelesh Guest

    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.
     
    Neelesh, Nov 9, 2005
    #4
  5. Neelesh

    Neelesh Guest

    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.
     
    Neelesh, Nov 9, 2005
    #5
  6. 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
     
    Jonathan Mcdougall, Nov 9, 2005
    #6
  7. Neelesh

    shikn Guest

    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:...
    > I was reading TC++PL (Chapter 11, section 11.5.1 "finding friends")
    > where it is mentioned that
    >
    >
    > 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?
    >
     
    shikn, Nov 9, 2005
    #7
  8. Neelesh

    Neelesh Guest

    > 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.
     
    Neelesh, Nov 9, 2005
    #8
  9. Neelesh

    Sumit Rajan Guest

    "Neelesh" <> wrote in message
    news:...

    >
    > /********** 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 <>
     
    Sumit Rajan, Nov 9, 2005
    #9
  10. Neelesh

    Neelesh Guest

    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.
     
    Neelesh, Nov 9, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dominic
    Replies:
    0
    Views:
    437
    Dominic
    Nov 3, 2004
  2. MoCha
    Replies:
    3
    Views:
    336
    Frank Schmitt
    Feb 17, 2004
  3. =?Utf-8?B?UmJydA==?=

    Friend class scope - what am I missing?

    =?Utf-8?B?UmJydA==?=, Nov 8, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    490
    =?Utf-8?B?UmJydA==?=
    Nov 8, 2007
  4. Peter
    Replies:
    8
    Views:
    2,184
    James Kanze
    Dec 20, 2010
  5. Peter
    Replies:
    2
    Views:
    277
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page