2012-05-11 6 views
2

これは私のプロジェクトのサンプルコードです。auto_ptrをフレンドクラスにする

プライベートメンバーにアクセスできるように、std :: auto_ptrをフレンドクラスにする必要があります。

#include "stdafx.h" 
    #include <map> 
    #include <iostream> 

    //sample namespace 
    namespace test 
    { 
    //class A 
    class A 
    { 
    public: 
     //making class B to friend , so that it can access private members 
     friend class B; 

    private: 
    int i; 

    //constructor - private 
    A(int in) 
     { 
     i = in; 
     } 
     // private destructor; 
     ~A() 
     { 
     std::cout<<"\n Ending"; 
     getchar(); 
    } 
    }; 

    //map to store A pointer 
    typedef std::map<int, std::auto_ptr<A>> MAP; 

    //class B, friend of A 
    class B 
    { 
    private: 
    MAP Map; 
    public: 
    //making auto_ptr to a friend class , so that it can call the destruct all the A  pointer. 
    friend class std::auto_ptr; //Getting error like" error C2990: 'std::auto_ptr' 
//: non-class template has already been declared as a class template 
    B() 
    { 
    std::auto_ptr<A> a(new A(1)); 
    std::auto_ptr<A> b(new A(2)); 
    std::auto_ptr<A> c(new A(3)); 
    Map[0] = a; 
    Map[1] = b; 
    Map[2] = c; 
    } 
    ~B() 
    { 

    } 
}; 
} 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    using namespace test; 
    B ab; 
return 0; 
} 

しかし、私はエラーを取得しています友人にそれを作るしようとしたときに... ....

答えて

3

を助けてくださいauto_ptrはテンプレートクラスなので、あなたのような何か必要があります

friend std::auto_ptr<B>; 
関連する問題