鱼C论坛

 找回密码
 立即注册
查看: 1931|回复: 3

[已解决]关于智能指针的使用

[复制链接]
发表于 2019-3-1 19:12:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)) ===|
最佳答案
2019-3-4 17:56:47
指针转换有问题,请注意注释:
#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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-3-4 08:19:46 | 显示全部楼层
你确定照着书弄的?拍张书的图上来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-4 17:28:42 | 显示全部楼层
本帖最后由 行客 于 2019-3-6 10:37 编辑

本帖莫名其妙需要审核才通过,清除重复内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-4 17:56:47 | 显示全部楼层    本楼为最佳答案   
指针转换有问题,请注意注释:
#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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-3 10:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表