c++ unique_ptr inside vector 继承
c++ unique_ptr inside vector with inheritance
我正在尝试使用不同的智能指针,运行 遇到了问题。
我有一个 Environment
摘要 class 和一个继承 Environment
:
的基础 class
class Ground : public Environment
{
protected:
std::string type;
int damage;
public:
Ground() : Environment()
{
this->type = "ground";
}
virtual void SetDamage(int _damage)
{
this->damage = _damage*5;
}
virtual std::string& GetType()
{
return this->type;
}
virtual int GetDamage()
{
return this->damage-10;
}
virtual ~Ground(){}
};
我还有一个 Dirt
class 继承了 Ground
class:
class Dirt : public Ground
{
public:
Dirt() : Ground()
{
this->type = "dirt";
}
void SetDamage(int _damage)
{
this->damage = _damage*6;
}
int GetDamage()
{
return this->damage-20;
}
~Dirt()
{
}
private:
};
现在如果我想像这样在 std::vector
中使用它:
std::vector<std::unique_ptr<Ground>> listGround;
std::unique_ptr<Ground> ground(new Ground());
listGround.push_back(ground);
std::unique_ptr<Dirt> dirt(new Dirt());
listGround.push_back(dirt); // FAIL
for (auto i = listGround.begin(); i != listGround.end(); i++)
{
(*i)->SetDamage(80);
std::cout << (*i)->GetType() << " " << (*i)->GetDamage() << std::endl;
}
listGround.empty();
我收到一个编译错误,指出在上面代码中标记为 FAIL 的行上没有可用的用户定义转换运算符可以执行此转换等等。
如果我使用 std::shared_ptr
,一切都会按预期进行。原始指针也是如此。
为什么会出现此错误?
error C2664: 'void std::vector<_Ty>::push_back(std::unique_ptr
&&)' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to
'std::unique_ptr<_Ty> &&' 1> with 1> [ 1>
_Ty=std::unique_ptr 1> ] 1> and 1> [ 1> _Ty=Dirt 1> ] 1> and 1> [
1> _Ty=Ground 1> ] 1> Reason: cannot
convert from 'std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty>' 1>
with 1> [ 1> _Ty=Dirt 1> ] 1>
and 1> [ 1> _Ty=Ground 1> ] 1>
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called
就地创建事物:
listGround.emplace_back(new Dirt());
它不是 shared_ptr
,但您尝试在 dirt
和 listGround.back()
之间共享所有权
std::vector::push_back
要求传递的类型是可复制的(或者在 C++11 中是可移动的),而 std::unique_ptr
是不可复制的。这就是错误消息告诉您它缺少相应的移动转换。您可以使用 std::move
函数移动 std::unique_ptr
,该函数只是将适当的类型转换为正确的 r-reference 类型。
我正在尝试使用不同的智能指针,运行 遇到了问题。
我有一个 Environment
摘要 class 和一个继承 Environment
:
class Ground : public Environment
{
protected:
std::string type;
int damage;
public:
Ground() : Environment()
{
this->type = "ground";
}
virtual void SetDamage(int _damage)
{
this->damage = _damage*5;
}
virtual std::string& GetType()
{
return this->type;
}
virtual int GetDamage()
{
return this->damage-10;
}
virtual ~Ground(){}
};
我还有一个 Dirt
class 继承了 Ground
class:
class Dirt : public Ground
{
public:
Dirt() : Ground()
{
this->type = "dirt";
}
void SetDamage(int _damage)
{
this->damage = _damage*6;
}
int GetDamage()
{
return this->damage-20;
}
~Dirt()
{
}
private:
};
现在如果我想像这样在 std::vector
中使用它:
std::vector<std::unique_ptr<Ground>> listGround;
std::unique_ptr<Ground> ground(new Ground());
listGround.push_back(ground);
std::unique_ptr<Dirt> dirt(new Dirt());
listGround.push_back(dirt); // FAIL
for (auto i = listGround.begin(); i != listGround.end(); i++)
{
(*i)->SetDamage(80);
std::cout << (*i)->GetType() << " " << (*i)->GetDamage() << std::endl;
}
listGround.empty();
我收到一个编译错误,指出在上面代码中标记为 FAIL 的行上没有可用的用户定义转换运算符可以执行此转换等等。
如果我使用 std::shared_ptr
,一切都会按预期进行。原始指针也是如此。
为什么会出现此错误?
error C2664: 'void std::vector<_Ty>::push_back(std::unique_ptr &&)' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty> &&' 1> with 1> [ 1>
_Ty=std::unique_ptr 1> ] 1> and 1> [ 1> _Ty=Dirt 1> ] 1> and 1> [ 1> _Ty=Ground 1> ] 1> Reason: cannot convert from 'std::unique_ptr<_Ty>' to 'std::unique_ptr<_Ty>' 1>
with 1> [ 1> _Ty=Dirt 1> ] 1>
and 1> [ 1> _Ty=Ground 1> ] 1>
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
就地创建事物:
listGround.emplace_back(new Dirt());
它不是 shared_ptr
,但您尝试在 dirt
和 listGround.back()
std::vector::push_back
要求传递的类型是可复制的(或者在 C++11 中是可移动的),而 std::unique_ptr
是不可复制的。这就是错误消息告诉您它缺少相应的移动转换。您可以使用 std::move
函数移动 std::unique_ptr
,该函数只是将适当的类型转换为正确的 r-reference 类型。