2016-01-25 30 views
-1

我使用模板元編程建立一個實體組分系統我不斷收到任何Cannot convert from [base type] to [type user requested]&Cannot convert NullComponent to [type user requested]&錯誤:「無法從‘A’轉換爲‘B&’

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

OR

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; 
} 
+0

分析循環,如果你發佈一個完整的樣本會更容易些,你想編譯(當前代碼丟失包括,IComponent和NullComponent)。 – Rumburak

+0

你忘了在decltype中尊重'c'嗎? - >'std :: is_same ()。value' - >'std :: is_same

+0

在「似乎現在工作「解決方案,爲什麼你需要'std :: is_base_of'和'std :: is_same'? dynamic_cast不是照顧你需要的一切嗎?另外,我非常確定'is_base_of'中的引用停止了代碼的正常工作。 – Rumburak

回答

2

至少三件事情:

  • GetComponent()你迭代unique_ptr元素,並比較它們與其他在std::is_same什麼類型(總是std::unique_ptr<IComponent>)。你可能不希望這樣。
  • 看來,您正在返回最終回報中的臨時參考。
  • return *c需要一個dynamic_cast除非C == IComponent。

編輯

另外:

  • std::is_base_of使得與引用沒有意義的。即使使用class NullComponent : IComponent {};,您仍可獲得std::is_base_of<IComponent&, NullComponent&>::value == false
  • 你不檢查nullptr

最後,在我看來,你應該更換你與

for(auto& component : _components) { 
    auto* c = dynamic_cast<C*>(component.get()); 
    if (c) 
    { 
    return *c; 
    } 
} 
+0

見編輯....... – Casey

0

在高層次上,從我可以看出,返回類型不能用於定義模板類型。參數列表可用於定義模板類型。

所以,舉例來說,這可能工作 -

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

希望這有助於。

+0

對不起!錯字! :) – buchipper

相關問題