C++ 将自动添加到好友类

C++ 将自动添加到好友类,c++,friend,auto-ptr,C++,Friend,Auto Ptr,这是我的项目的示例代码 我必须将std::auto_ptr设置为一个friend类,以便它能够访问私有成员 #include "stdafx.h" #include <map> #include <iostream> //sample namespace namespace test { //class A class A { public: //making class B to friend , s

这是我的项目的示例代码

我必须将std::auto_ptr设置为一个friend类,以便它能够访问私有成员

  #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;
}
#包括“stdafx.h”
#包括
#包括
//示例名称空间
名称空间测试
{
//甲级
甲级
{
公众:
//使B类成为好友,以便它可以访问私人成员
B级朋友;
私人:
int i;
//构造函数-专用
A(整数英寸)
{
i=英寸;
}
//私有析构函数;
~A()
{

std::cout因为
auto_ptr
是一个模板类,所以您需要如下内容:

friend std::auto_ptr<B>;
friend std::auto_ptr;