我正在loadAsset我的資產管理器實施中實施方法。我嘗試使用具有以下簽名的可變引數模板方法:
class AssetManager {
public:
template <typename AssetType, typename... Args>
void loadAsset(std::string_view name, const Args &...args);
...
};
.cpp并在檔案中進行以下專業化:
template <>
void AssetManager::loadAsset<Texture, std::string>(std::string_view name, const std::string &path) {...}
template void AssetManager::loadAsset<Texture, std::string>(std::string_view, const std::string &path); //explicit instantiation
但是,當嘗試loadAsset像這樣呼叫方法時:
m_assetManager->loadAsset<NOX::Texture>("texture", "assets/textures/texture.jpg");
我收到以下錯誤:
error LNK2019: unresolved external symbol "public: void __cdecl NOX::AssetManager::loadAsset<class NOX::Texture,char [28]>(class std::basic_string_view<char,struct std::char_traits<char> >,char const (&)[28])" (??$loadAsset@VTexture@NOX@@$$BY0BM@D@AssetManager@NOX@@QEAAXV?$basic_string_view@DU?$char_traits@D@std@@@std@@AEAY0BM@$$CBD@Z) referenced in function "public: __cdecl ExampleApplication::ExampleApplication(struct NOX::ApplicationSpecification const &)" (??0ExampleApplication@@QEAA@AEBUApplicationSpecification@NOX@@@Z)
我怎樣才能防止這種情況發生?我正在使用 C 17,不能使用 C 20。
uj5u.com熱心網友回復:
如果您在 cpp 檔案中專門化一個模板,則該模板將在給定檔案中可用,但在外部不可用(當然,除非您手動實體化它)。最好的方法是將所有模板定義移動到標題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/511325.html
標籤:C 模板可变参数模板
下一篇:平均分(柬埔寨偶像練習)
