-1

テンプレート メタプログラミングを使用してエンティティ コンポーネント システムを構築しています。Cannot convert from [base type] to [type user requested]&またはCannot convert NullComponent to [type user requested]&エラーが発生し続けます:

class Entity {
public:
    Entity() = default;
    ~Entity() = default;

    template<typename C, typename... Args>
    void AddComponent(Args&&... args);

    template<typename C>
    C& GetComponent();

protected:
private:
    //...add/get helper methods here...

    unsigned int _id;
    std::vector<std::unique_ptr<IComponent>> _components;
};

template<typename C>
C& Entity::GetComponent() {
    for(auto c : _components) {
        if(std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
            return *c; //<-- error here
        }
    }
    return NullComponent(); //<-- and here
}

編集

これらのオプションは今のところ機能しているようです。

template<typename C>
const C& Entity::GetComponent() const {
    for(auto& uc : _components) {
        auto* c = dynamic_cast<C*>(uc.get());
        if(c && std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
            return *c;
        }
    }
    throw std::runtime_error(std::string("Component not available."));
}

また

class Entity {
public:
    //same as before...
protected:
private:
    //same as before...
    a2de::NullComponent _null_component;
};

template<typename C>
const C& Entity::GetComponent() const {
    for(auto& uc : _components) {
        auto* c = dynamic_cast<C*>(uc.get());
        if(c && std::is_base_of<a2de::IComponent&, C&>().value && std::is_same<decltype(c), C&>().value) {
            return *c;
        }
    }
    return _null_component;
}
4

2 に答える 2

2

少なくとも 3 つのこと:

  • で要素GetComponent()を反復処理しunique_ptr、それらの型 (常にstd::unique_ptr<IComponent>) を 内の他のものと比較しstd::is_sameます。あなたはおそらくそれを望んでいません。
  • 最終リターンで一時的なものへの参照を返しているようです。
  • return *cC == IComponent でない限り、dynamic_cast が必要です。

編集

また:

  • std::is_base_of参照では意味がありません。であってもclass NullComponent : IComponent {};、あなたはまだ得るでしょうstd::is_base_of<IComponent&, NullComponent&>::value == false
  • そして、あなたは nullptr をチェックしません

最後に、 for ループを次のように置き換える必要があるようです

for(auto& component : _components) {
  auto* c = dynamic_cast<C*>(component.get());
  if (c)
  {
    return *c;
  }
}
于 2016-01-25T06:25:03.447 に答える