C ++错误:无法使用类型的值初始化类型的引用 [英] C++ error: A reference of type cannot be initialized with a value of type

查看:1671
本文介绍了C ++错误:无法使用类型的值初始化类型的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个找不到解决方案的问题。我正在创建具有智能指针的实体-组件系统。

Hi I got a problem I can't find a solution to. I'm creating a entity - component system with smart pointers.

我得到的错误是:


类型为 std :: unique_ptr< PrimaryComponent,std :: default_delete< PrimaryComponent>的引用& (非const限定)不能使用类型 std :: unique_ptr< PlayerGraphics,std :: default_delete< PlayerGraphics>>

a reference of type std::unique_ptr<PrimaryComponent, std::default_delete<PrimaryComponent>> & (not const-qualified) cannot be initialized with a value of type std::unique_ptr<PlayerGraphics, std::default_delete<PlayerGraphics>>

游戏世界类:

auto GameWorld::Setup_World() -> void
{
    player->Attach_Component(player_Graphics);
    Add_GameObject(player);
}

gameobject类附加组件函数:

void GameObject::Attach_Component(std::unique_ptr<PrimaryComponent> &component)
{
    if (component != nullptr)
    {
        component_container.push_back(component);
    }
}

player_Graphics和player的声明:

class GameWorld
{
private:
    std::vector<std::unique_ptr<GameObject>> gameObject_List;
    std::unique_ptr<GameObject> player;
    std::unique_ptr<PlayerGraphics> player_Graphics

player GameObject player_Graphics 是从 PrimaryComponent 派生的图形组件。

player is a GameObject, player_Graphics is a Graphics Component which derives from PrimaryComponent.

主要组件类:

class PrimaryComponent
{
protected:
public:
    PrimaryComponent();
    virtual ~PrimaryComponent();
    virtual void Render(GameObject &gameObject) = 0;
    virtual void Update(GameObject &gameObject, GameWorld &gameWorld, float gameTime) = 0;
};

PlayerGraphics类:

class PlayerGraphics :
    public GraphicsComponent
{
public:
    PlayerGraphics();
    virtual ~PlayerGraphics();
    virtual void Render(GameObject &gameObject);
    virtual void Update(GameObject &gameObject, GameWorld &gameWorld, float gameTime);
};

PlayerGraphics继承自GraphicsComponent,而GraphicsComponent继承自PrimaryComponent。

PlayerGraphics derives from GraphicsComponent which derives from PrimaryComponent.

推荐答案

问题出在引用中:

该行:

void GameObject::Attach_Component(std::unique_ptr<PrimaryComponent> &component)

应为:

void GameObject::Attach_Component(std::unique_ptr<PrimaryComponent> component)

然后,当您调用它时,而不是:

And then, when you call it, instead of:

player->Attach_Component(player_Graphics);

您应该这样做:

player->Attach_Component(std::move(player_Graphics));

也就是说,该函数应该拥有指针的所有权。请注意,原始 player_Graphics 将设置为null(将其移入,如果要保留该对象的引用,则使用了错误的工具)。

That is, the function should take ownership of the pointer. Note that the original player_Graphics will be set to null (you moved it in, if you want to keep a reference ot that object then you are using the wrong tool).

您可能想知道为什么在代码中传递 std :: unique_ptr< PrimaryComponent> 直接起作用,但是传递 std :: unique_ptr< PlayerGraphics> 不会。很好的解释是,您正在使用非常量引用,并且非常量引用只能绑定到完全相同时间的对象。但是,如果您按值传递参数,则智能指针将被移动,并且一切都将正常工作。

You may be wondering why in your code passing a std::unique_ptr<PrimaryComponent> directly works, but passing a std::unique_ptr<PlayerGraphics> does not. Well the explanation is that you are using a non-const reference, and non-const references can only bind to an object of the exact same time. However if you pass the argument by value, then the smart pointer will be moved and it all will just work.

这一切都说得通, std :: unique_ptr 是不可复制,不可共享,可移动的智能指针。

This all makes sense, std::unique_ptr is meant to be non-copyable, non-shareable, moveable smart pointers. You are expected to move the pointer around, not to pass it by reference.

TL; DR:如果 B A 的子类,您可以将 std :: unique_ptr< B> 移到 std :: unique_ptr< A> ,但是您不能将 std :: unique_ptr< B> 绑定到类型为的引用std :: unique_ptr< a&

TL;DR: If B is a subclass of A you can move a std::unique_ptr<B> into a std::unique_ptr<A> but you cannot bind a std::unique_ptr<B> to a reference of type std::unique_ptr<a>&.

PS。移动和广播的构造函数声明如下:

PS. The constructor to move&cast is declared as follows:

template <class T, class D = default_delete<T>>
class unique_ptr
{
    template <class U, class E>
    unique_ptr (unique_ptr<U,E>&& x) noexcept;
};

这篇关于C ++错误:无法使用类型的值初始化类型的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆