| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- #include<iostream>
 
 - #include<string>
 
 - #include<memory>  //  智能指针的头文件
 
  
- int main()
 
 - {
 
 -     std::auto_ptr<std::string>  pf[5]={ std::auto_ptr<std::string>(new std::string("@@@@@@@")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("#######")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("*************")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("%%%%%%%%%%")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("$$$$"))   };
 
 -     std::shared_ptr<std::string>pw;
 
  
-     pw=pf[2];
 
  
-     std::cout<<"The nominees for best avian baseball fp are\n";
 
  
-     for(int i=0; i<5; i++)
 
 -     {
 
 -         std::cout<<*pf[i]<<"\n";
 
 -     }
 
  
-     std::cout<<"The winnwr is: "<<*pw<<"\n";
 
  
-     std::cin.get();
 
  
-     return 0;
 
 - }
 
 
  复制代码 
 
照着书上打的代码,为什么会报错 
 
|In function 'int main()':| 
|15|error: cannot bind 'std::auto_ptr<std::basic_string<char> >' lvalue to 'std::auto_ptr<std::basic_string<char> >&&'| 
|282|error:   initializing argument 1 of 'std::shared_ptr<_Tp>& std::shared_ptr<_Tp>::operator=(std::auto_ptr<_Tp1>&&) [with _Tp1 = std::basic_string<char>; _Tp = std::basic_string<char>; std::shared_ptr<_Tp> = std::shared_ptr<std::basic_string<char> >]'| 
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 
指针转换有问题,请注意注释: 
 
- #include<iostream>
 
 - #include<string>
 
 - #include<memory>  //  智能指针的头文件
 
  
- int main()
 
 - {
 
 -     std::auto_ptr<std::string>  pf[5]={ std::auto_ptr<std::string>(new std::string("@@@@@@@")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("#######")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("*************")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("%%%%%%%%%%")) ,
 
 -                                          std::auto_ptr<std::string>(new std::string("$"))   };
 
 -         std::shared_ptr<std::string> pw(pf[2].get());        //将pf[2]转换为普通指针后,赋值给pw
 
  
-     //pw = pf[2];
 
  
-     std::cout<<"The nominees for best avian baseball fp are\n";
 
  
-     for(int i=0; i<5; i++)
 
 -     {
 
 -         std::cout<<*pf[i]<<"\n";
 
 -     }
 
  
-     std::cout<<"The winnwr is: "<<*pw<<"\n";
 
  
-     std::cin.get();
 
  
-     return 0;
 
 - }
 
  复制代码 
 
 
 |   
 
 
 
 |