C++ 如何使用自定义删除器分配唯一的\u ptr
我试图传递一个指向某个函数的指针,该函数在结构中为传入的指针设置一个C++ 如何使用自定义删除器分配唯一的\u ptr,c++,c++11,struct,unique-ptr,allegro,C++,C++11,Struct,Unique Ptr,Allegro,我试图传递一个指向某个函数的指针,该函数在结构中为传入的指针设置一个unique\u ptr。但是,我在函数的最后一行得到以下编译错误 错误C2280:“std::unique_ptr::unique_ptr(const std::unique_ptr,):尝试引用已删除的函数 c:\ProgramFiles(x86)\microsoft visual studio 12.0\vc\include\memory(1486):请参见“std::unique\u ptr::unique\u ptr”
unique\u ptr
。但是,我在函数的最后一行得到以下编译错误
错误C2280:“std::unique_ptrusing namespace std;
namespace std {
template<>
class default_delete < ALLEGRO_BITMAP > {
public:
void operator()(ALLEGRO_BITMAP* ptr) {
al_destroy_bitmap(ptr);
}
};
}
typedef struct {
unique_ptr<ALLEGRO_BITMAP> img;
} Skin;
typedef struct {
Skin skins[MAX_ENTITY_COUNT];
} World;
unsigned int createBlock(World world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
} // error on this line
使用名称空间std;
名称空间标准{
模板
类默认\u删除{
公众:
void运算符()(快板位图*ptr){
al_销毁_位图(ptr);
}
};
}
类型定义结构{
独特的ptr img;
}皮肤;
类型定义结构{
皮肤皮肤[最大实体数量];
}世界;
无符号int-createBlock(世界,快板位图*img){
无符号整数实体=newEntityIndex(世界);
world.skins[entity].img=make_unique(img);
返回实体;
}//这行有错误
感谢您的帮助。谢谢。在您的
createBlock
函数中,您按值获取World
,这意味着它将被复制。但是,您不能复制唯一的\u ptr
,因此这就是错误的来源。这也意味着在函数中设置unqiue\u ptr
不会有任何效果
相反,你应该参考世界:
unsigned int createBlock(World& world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
}
在
createBlock
函数中,您按值获取World
,这意味着它将被复制。但是,您不能复制唯一的\u ptr
,因此这就是错误的来源。这也意味着在函数中设置unqiue\u ptr
不会有任何效果
相反,你应该参考世界:
unsigned int createBlock(World& world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
}
在
createBlock
函数中,您按值获取World
,这意味着它将被复制。但是,您不能复制唯一的\u ptr
,因此这就是错误的来源。这也意味着在函数中设置unqiue\u ptr
不会有任何效果
相反,你应该参考世界:
unsigned int createBlock(World& world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
}
在
createBlock
函数中,您按值获取World
,这意味着它将被复制。但是,您不能复制唯一的\u ptr
,因此这就是错误的来源。这也意味着在函数中设置unqiue\u ptr
不会有任何效果
相反,你应该参考世界:
unsigned int createBlock(World& world, ALLEGRO_BITMAP* img) {
unsigned int entity = newEntityIndex(world);
world.skins[entity].img = make_unique<ALLEGRO_BITMAP>(img);
return entity;
}
1) 所给出的示例不是独立的。2) unique_ptr有某些要求(请参阅)。您不应该在此处使用
make_unique
。ALLEGRO_位图应仅通过以下方式使用:。您不应该直接创建ALLEGRO_位图对象,这正是make_unique
所要做的。您应该只使用unique\u ptr
构造函数。1)给出的示例不是自包含的。2) unique_ptr有某些要求(请参阅)。您不应该在此处使用make_unique
。ALLEGRO_位图应仅通过以下方式使用:。您不应该直接创建ALLEGRO_位图对象,这正是make_unique
所要做的。您应该只使用unique\u ptr
构造函数。1)给出的示例不是自包含的。2) unique_ptr有某些要求(请参阅)。您不应该在此处使用make_unique
。ALLEGRO_位图应仅通过以下方式使用:。您不应该直接创建ALLEGRO_位图对象,这正是make_unique
所要做的。您应该只使用unique\u ptr
构造函数。1)给出的示例不是自包含的。2) unique_ptr有某些要求(请参阅)。您不应该在此处使用make_unique
。ALLEGRO_位图应仅通过以下方式使用:。您不应该直接创建ALLEGRO_位图对象,这正是make_unique
所要做的。您应该只使用unique\u ptr
构造函数。