CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2009
    Posts
    1

    friend class is in none-namespace

    Hi,

    I'm looking for a solution with friend concept. The friend class is out of the namespace. It isn't in any namespace. So, is there a solution?

    Code:
    namespace b
    {
    
            class Test
            {
                    public:
                            friend class Test2;
                            ~Test(){};
                    private:
                            Test(){};
            };
    
    
    }; // end namespace b
    
    class Test2
    {
            public:
                    Test2(){};
                    ~Test2(){};
                    void test()
                    {
                            b::Test* t = new b::Test();
                    };
    
    };
    
    int main(){
            Test2 t;
            t.test();
            return 0;
    }
    Ouput:
    main.cpp: In member function ‘void Test2::test()’:
    main.cpp:11: error: ‘b::Test::Test()’ is private
    main.cpp:24: error: within this context

    Thank you very much for your time.

    greetings

  2. #2
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: friend class is in none-namespace

    Quote Originally Posted by skywalk View Post
    Hi,

    I'm looking for a solution with friend concept. The friend class is out of the namespace. It isn't in any namespace. So, is there a solution?

    Code:
    class Test2;
    namespace b
    {
    
            class Test
            {
                    public:
                            friend class Test2;
                            ~Test(){};
                    private:
                            Test(){};
            };
    
    
    }; // end namespace b
    
    class Test2
    {
            public:
                    Test2(){};
                    ~Test2(){};
                    void test()
                    {
                            b::Test* t = new b::Test();
                    };
    
    };
    
    int main(){
            Test2 t;
            t.test();
            return 0;
    }
    Ouput:
    main.cpp: In member function ‘void Test2::test()’: //
    main.cpp:11: error: ‘b::Test::Test()’ is private
    main.cpp:24: error: within this context

    Thank you very much for your time.

    greetings
    Hello there
    you need to make the friend declaration visible outside the namespace.
    When you use new in C++, you must call delete, or else you have a memory leak.

    ..and um namespace doesn't end with a semi-colon

    bye
    Last edited by potatoCode; April 9th, 2009 at 10:36 AM. Reason: fixed something

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured