构造shared_ptr时出现问题
Trouble constructing shared_ptr
我是智能指针的新手,我正在处理每一个绊脚石。
我有一个结构texture_t
:
struct texture_t
{
hash32_t hash;
uint32_t width;
uint32_t height;
uint32_t handle;
};
当我尝试使用以下行对此结构进行shared_ptr
时:
auto texture_shared_ptr = std::make_shared<texture_t>(new texture_t());
我得到这个错误:
error C2664: 'mandala::texture_t::texture_t(const mandala::texture_t &)' : cannot convert parameter 1 from 'mandala::texture_t *' to 'const mandala::texture_t &'
这个错误是从哪里来的?我该如何避免它?
std::make_shared<T>(args...)
的目的是分配一个用参数args...
构造的T
对象。此操作背后的思想是std::shared_ptr<T>
在概念上维护两个分配的对象:
- 指向类型
T
的指针 - 跟踪当前
std::shared_pt<T>
的数量和引用该对象的std::weak_ptr<T>
对象的数量的记录
当构造std::shared_ptr<T>
时,构造函数进行第二次分配来构造用于其内部记账的记录。std:make_shared<T>(args...)
只进行一次内存分配。
您看到的错误源于尝试使用mandala::texture_t*
构造mandala::texture_t
,但mandala::texture_t
只有一个参数构造函数是复制构造函数。但是,指针不能作为复制构造函数的参数。
不应该将new
ed指针传递给std::make_shared
。您只需要传递参数,就可以从中构造texture_t
。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 从const ptr*转换为ptr*时出现问题