How to reference out of namespace friend

Discussion in 'C++' started by edgekaos, Nov 8, 2005.

  1. edgekaos

    edgekaos Guest

    This doesn't seem to work in VC.Net. cpp file that include something
    like this would have a ambiguous symbol problem with two MyFriend
    declarations. Any ideas?

    namespce mynamespace
    {
    class MyClass
    {
    friend class MyFriend;
    };
    };

    class MyFriend
    {
    };
     
    edgekaos, Nov 8, 2005
    #1
    1. Advertising

  2. edgekaos wrote:
    > This doesn't seem to work in VC.Net. cpp file that include something
    > like this would have a ambiguous symbol problem with two MyFriend
    > declarations. Any ideas?
    >
    > namespce mynamespace


    Thats should be "namespace".

    > {
    > class MyClass
    > {
    > friend class MyFriend;
    > };
    > };


    Remove that semicolumn.

    >
    > class MyFriend
    > {
    > };


    This

    namespace mynamespace
    {
    class MyClass
    {
    friend class MyFriend;
    };
    }

    class MyFriend
    {
    };

    int main()
    {
    mynamespace::MyClass c;
    MyFriend f;
    }

    compiles fine. Looks like the code you posted isn't the one causing the
    problem.

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.8

    Jonathan
     
    Jonathan Mcdougall, Nov 8, 2005
    #2
    1. Advertising

  3. edgekaos

    edgekaos Guest

    Sorry I was lazy just made up the sample during the post. Here is the
    correct version

    namespace MyNameSpace
    {
    class MyClass
    {
    friend class MyFriend;
    int member;
    };
    }

    class MyFriend
    {
    MyFriend()
    {
    MyNameSpace::MyClass myClass;
    myClass.member = 1;
    }
    };

    name this as MyClass.cpp and compile cause:

    MyClass.cpp(15): error C2248: 'MyNameSpace::MyClass::member' : cannot
    access private member declared in class 'MyNameSpace::MyClass'

    My guess is that I need to change this line:
    friend class MyFriend;
    to something along the lines of:
    friend class ::MyFriend;
    to tell compiler MyFriend is in the global namespace.
     
    edgekaos, Nov 8, 2005
    #3
  4. edgekaos wrote:
    > Sorry I was lazy just made up the sample during the post. Here is the
    > correct version


    Please, quote the message you are answering to.

    > namespace MyNameSpace
    > {
    > class MyClass
    > {
    > friend class MyFriend;


    This is equivalent to

    friend class MyNameSpace::MyFriend;

    which is clearly not what you want. You'll have to qualify it:

    friend class ::MyFriend;

    > int member;
    > };
    > }
    >
    > class MyFriend
    > {
    > MyFriend()
    > {
    > MyNameSpace::MyClass myClass;
    > myClass.member = 1;
    > }
    > };
    >
    > name this as MyClass.cpp and compile cause:
    >
    > MyClass.cpp(15): error C2248: 'MyNameSpace::MyClass::member' : cannot
    > access private member declared in class 'MyNameSpace::MyClass'
    >
    > My guess is that I need to change this line:
    > friend class MyFriend;
    > to something along the lines of:
    > friend class ::MyFriend;
    > to tell compiler MyFriend is in the global namespace.


    Yes.


    Jonathan
     
    Jonathan Mcdougall, Nov 8, 2005
    #4
  5. Jonathan Mcdougall wrote:
    > edgekaos wrote:
    > > Sorry I was lazy just made up the sample during the post. Here is the
    > > correct version

    >
    > Please, quote the message you are answering to.
    >
    > > namespace MyNameSpace
    > > {
    > > class MyClass
    > > {
    > > friend class MyFriend;

    >
    > This is equivalent to
    >
    > friend class MyNameSpace::MyFriend;
    >
    > which is clearly not what you want. You'll have to qualify it:
    >
    > friend class ::MyFriend;


    But note that now because the class name is qualified, it is searched
    only in that scope. If it is not found, the program is ill-formed.
    You'll have to declare ::MyFriend before that point:

    class MyFriend;

    namespace MyNameSpace
    {
    class MyClass
    {
    friend class ::MyFriend;
    int member;
    };
    }


    Jonathan
     
    Jonathan Mcdougall, Nov 8, 2005
    #5
    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. Alexander Grigoriev
    Replies:
    6
    Views:
    2,583
    Kevin Goodsell
    Sep 12, 2003
  2. Tim Partridge
    Replies:
    2
    Views:
    619
    Nick Hounsome
    Jan 8, 2004
  3. Tim Partridge
    Replies:
    1
    Views:
    720
    Sumit Rajan
    Jun 2, 2004
  4. marco_segurini
    Replies:
    4
    Views:
    1,034
    Rob Williscroft
    Jun 16, 2004
  5. Peter
    Replies:
    2
    Views:
    310
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page