unique_ptr参数。错误C2664

时间:2014-08-06 20:52:43

标签: c++ c++11

我最近刚刚听说过C ++ 11中的新unique_ptr并想尝试一下。

我改变了这段代码:

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);
}

到这个

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);
}

现在我收到了这个错误:

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作为参数?

提前致谢。


太短的消息......

1 个答案:

答案 0 :(得分:2)

你应该重新考虑使用memset(并且只是对它进行值初始化,例如new SomeClass()),但在一般情况下,如果你想传递unique_ptr的内部指针(例如,遗留函数)使用其get函数。