我無法將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;
}
add()的實現在哪裏?它找不到add()的定義 –
複製了錯誤的,對不起,編輯 – Treycos
我不認爲這將甚至工作,因爲你的模板參數與聲明的模板參數不匹配,你應該在頭文件中定義模板函數文件不在cpp –