Problem with friend class declaration in a namespace

This is a discussion on Problem with friend class declaration in a namespace within the C++ Programming forums, part of the General Programming Boards category; I have a problem declaring a friend class in the following abstract code Code: namespace qf { class Class { ...

  1. #1
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115

    Problem with friend class declaration in a namespace

    I have a problem declaring a friend class in the following abstract code
    Code:
    namespace qf {
    
    class Class {
    friend class FriendClass;
    protected:
        bool member;
    };
    
    }
    ....
    void FriendClass::foo(qf::Class &cls) {
         cls.member = true;
    }
    And the error I get is "qf::class::member is protected in this context. I tried doing things like
    Code:
    friend class ::FriendClass;
    and
    Code:
    class FriendClass;
    
    namespace qf {
    
    class Class {
    friend class FriendClass;
    But those just gave errors or didn't work.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,668
    This should compile:
    Code:
    class FriendClass;
    
    namespace qf
    {
        class Class
        {
            friend class ::FriendClass;
            bool member;
        public:
            Class() : member(false) {}
        };
    }//namespace qf
    
    class FriendClass
    {
    public:
        FriendClass(qf::Class &c) {c.member = true;}
    };
    
    int main()
    {
        qf::Class c;
        FriendClass f(c);
        return 0;
    }
    >> I tried doing..
    Combine your attempts into one try

    gg

  3. #3
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Ah, I see. Both of my attempts on their own were wrong, but the both of them together is the solution. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in pass a variable to a class
    By nima_pw in forum C# Programming
    Replies: 3
    Last Post: 06-09-2009, 07:30 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Class Membership Problem
    By josephjah in forum C++ Programming
    Replies: 5
    Last Post: 05-27-2007, 01:48 PM
  4. Predeclaration of template class in namespace
    By unregistred in forum C++ Programming
    Replies: 0
    Last Post: 05-30-2003, 03:52 AM
  5. friend Class Help
    By PURE_POWER in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2002, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21