2016-10-25 99 views
0

我無法將sf :: Text,sf :: Sprite和sf :: Text的shared_ptr放入shared_ptr的數組中
sf :: Drawable是所有以上是抽象
這裏的遊戲引擎功能:使用模板的智能指針的多態性

GameEngine.h

template<class T> 
    void add(std::shared_ptr<T> actor); 

GameEngine.cpp

template<> 
void GameEngine::add<sf::Drawable>(std::shared_ptr<sf::Drawable> actor) 
{ 
    drawables.insert(actor); 
} 

Rendering.cpp

void Rendering::addDrawable(std::shared_ptr<sf::Sprite> sprite, sf::Vector2i texture_rect, const char* texture_name) 
{ 
    sf::Texture* _texture = engine.lock()->getGameData().getTexture(texture_name); 
    if (_texture) { 
     sprite->setTexture(*_texture); 
     sprite->setTextureRect(sf::IntRect(0, 0, texture_rect.x, texture_rect.y)); 
     //What do i have to put in the below function? 
     engine.lock()->add(sprite); 
    } 
} 

,我發現了以下錯誤在2015年VS:

1>Rendering.obj : error LNK2019: symbole externe non résolu "public: void __thiscall GameEngine::add<class sf::Sprite>(class std::shared_ptr<class sf::Sprite>)" ([email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@@[email protected]@@Z) référencé dans la fonction "public: void __thiscall Rendering::addDrawable(class std::shared_ptr<class sf::Sprite>,class sf::Vector2<int>,char const *)" ([email protected]@@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]) 
1>Rendering.obj : error LNK2019: symbole externe non résolu "public: void __thiscall GameEngine::add<class sf::Shape>(class std::shared_ptr<class sf::Shape>)" ([email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@@[email protected]@@Z) référencé dans la fonction "public: void __thiscall Rendering::addDrawable(class std::shared_ptr<class sf::Shape>,char const *)" ([email protected]@@[email protected]@[email protected]@@[email protected]@[email protected]) 
1>Rendering.obj : error LNK2019: symbole externe non résolu "public: void __thiscall GameEngine::add<class sf::Text>(class std::shared_ptr<class sf::Text>)" ([email protected][email protected]@@@[email protected]@[email protected]@[email protected]@@[email protected]@@Z) référencé dans la fonction "public: void __thiscall Rendering::addDrawable(class std::shared_ptr<class sf::Text>,char const *,char const *,class sf::Color)" ([email protected]@@[email protected]@[email protected]@@[email protected]@[email protected]@@@Z) 

有沒有把一個子類的共享指針到共享指針數組的方式其母親班?

編輯:
的回答說:

只是因爲雪碧是可繪製的子類,並不意味着 shared_ptr<Sprite>shared_ptr<Drawable>一個子類。

那我爲什麼要這樣做呢?

class A{}; 

class B : public A {}; 

int main() { 
    std::vector<std::shared_ptr<A>> ar; 
    ar.push_back(std::make_shared<B>()); 
    return 0; 
} 
+0

add()的實現在哪裏?它找不到add()的定義 –

+0

複製了錯誤的,對不起,編輯 – Treycos

+0

我不認爲這將甚至工作,因爲你的模板參數與聲明的模板參數不匹配,你應該在頭文件中定義模板函數文件不在cpp –

回答

2

只是因爲SpriteDrawable一個子類,並不意味着shared_ptr<Sprite>shared_ptr<Drawable>一個子類。

當編譯器看到這一點:

engine.lock()->add(sprite); 

它看起來繼該overload resolution規則的成員函數add(shared_ptr<sf::Sprite>)

儘管shared_ptr<Sprite>不是shared_ptr<Drawable>的子類,但存在隱式轉換,所以它將成爲候選。

然而這樣的:

template<class T> 
    void add(std::shared_ptr<T> actor); 

提供什麼樣的功能的聲明,但沒有定義。因此它提供了一個精確匹配,並由編譯器選擇。這就是你得到鏈接器錯誤的原因。要解決這個

的一種方法是使用std::static_pointer_cast,像這樣:

engine.lock()->add(std::static_pointer_cast<Drawable>(sprite)); 

我也將重新考慮是否/爲什麼你需要上面的模板 - 我看不出那是什麼服務宗旨,比其他給你鏈接時間錯誤而不是編譯時錯誤。

+0

@Treyscos看到大聲笑 –

+0

你毀了我的名字,小貓會爲此付出代價 – Treycos