首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >unique_ptr参数误差C2664

unique_ptr参数误差C2664
EN

Stack Overflow用户
提问于 2014-08-06 20:52:43
回答 1查看 1.7K关注 0票数 0

我最近刚刚听说了新的unique_ptr in C++11,并想试一试。

我更改了这个代码:

代码语言:javascript
复制
void CreateDummySpell(uint32 id)
{
    const char * name = "Dummy Trigger";
    SpellEntry * sp = new SpellEntry;
    memset(sp, 0, sizeof(SpellEntry));
    sp->Id = id;
    sp->Attributes = 384;
    sp->AttributesEx = 268435456;
    sp->AttributesExB = 4;
    sp->CastingTimeIndex=1;
    sp->procChance=75;
    sp->rangeIndex=13;
    sp->EquippedItemClass=uint32(-1);
    sp->Effect[0]=3;
    sp->EffectImplicitTargetA[0]=25;
    sp->NameHash=crc32((const unsigned char*)name, (unsigned int)strlen(name));
    sp->dmg_multiplier[0]=1.0f;
    sp->StanceBarOrder=-1;
    dbcSpell.SetRow(id,sp);
    sWorld.dummyspells.push_back(sp);
}

到这个

代码语言:javascript
复制
void CreateDummySpell(uint32 id)
{
    const char * name = "Dummy Trigger";
//  SpellEntry * sp = new SpellEntry;
    std::unique_ptr<SpellEntry> sp(new SpellEntry);
    memset(sp, 0, sizeof(SpellEntry));
    sp->Id = id;
    sp->Attributes = 384;
    sp->AttributesEx = 268435456;
    sp->AttributesExB = 4;
    sp->CastingTimeIndex=1;
    sp->procChance=75;
    sp->rangeIndex=13;
    sp->EquippedItemClass=uint32(-1);
    sp->Effect[0]=3;
    sp->EffectImplicitTargetA[0]=25;
    sp->NameHash=crc32((const unsigned char*)name, (unsigned int)strlen(name));
    sp->dmg_multiplier[0]=1.0f;
    sp->StanceBarOrder=-1;
    dbcSpell.SetRow(id,sp);
    sWorld.dummyspells.push_back(sp);
}

现在我得到了这个错误:

代码语言:javascript
复制
error C2664: 'void *memset(void *,int,size_t)': cannot convert argument 1 from 'std::unique_ptr<SpellEntry,std::default_delete<_Ty>>' to 'void *'
2>          with
2>          [
2>              _Ty=SpellEntry
2>          ]
2>  ..\..\src\arcemu-world\SpellFixes.cpp(29): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
2>..\..\src\arcemu-world\SpellFixes.cpp(43): error C2664: 'void DBCStorage<SpellEntry>::SetRow(uint32,T *)': cannot convert argument 2 from 'std::unique_ptr<SpellEntry,std::default_delete<_Ty>>' to 'SpellEntry *'
2>          with
2>          [
2>              T=SpellEntry
2>          ]
2>          and
2>          [
2>              _Ty=SpellEntry
2>          ]
2>  ..\..\src\arcemu-world\SpellFixes.cpp(43): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
2>..\..\src\arcemu-world\SpellFixes.cpp(44): error C2664: 'void std::list<SpellEntry *,std::allocator<_Kty>>::push_back(const _Ty &)': cannot convert argument 1 from 'std::unique_ptr<SpellEntry,std::default_delete<_Ty>>' to 'SpellEntry *&&'
2>          with
2>          [
2>              _Kty=SpellEntry *
2>  ,            _Ty=SpellEntry *
2>          ]
2>          and
2>          [
2>              _Ty=SpellEntry
2>          ]
2>  ..\..\src\arcemu-world\SpellFixes.cpp(44): note: Reason: cannot convert from 'std::unique_ptr<SpellEntry,std::default_delete<_Ty>>' to 'SpellEntry *'
2>          with
2>          [
2>              _Ty=SpellEntry
2>          ]
2>  ..\..\src\arcemu-world\SpellFixes.cpp(44): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

看来我可以把sp作为参数传递。如何使用unique_ptr智能指针传递sp作为参数?

提前谢谢。

短信太短了..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-06 20:55:58

您可能应该重新考虑使用memset (只需值-初始化它,例如new SomeClass() ),但是在一般情况下,如果您想传递unique_ptr的内部指针(例如指向遗留函数),则使用它的get函数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25169978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档