热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

C++中对象构造时的唯一指针错误

我在使用unique_ptr创建对象时出错,如下所示错误:从Account*到非标量类型std::unique_ptr的错误转换std::uniqu

我在使用 unique_ptr 创建对象时出错,如下所示错误:从 Account * 到非标量类型 std::unique_ptr 的错误转换

std::unique_ptr acc_ptr = new Account(100);

如果我使用如下原始指针,则没有错误

Account *acc_ptr = new Account(100);

为什么会这样?

回答

std::unique_ptr带指针的构造函数是explicit

你需要这个:

std::unique_ptr acc_ptr(new Account(100));

或者,从 C++14 开始,使用更好的std::make_unique版本:

auto acc_ptr = std::make_unique(100);






推荐阅读
author-avatar
weizhe
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有