我几乎不知道C++模板会发生什么,但我正在尝试实现一个函数,它在向量中搜索满足给定属性的元素(在这种情况下,搜索名称给定的元素).我在.h文件中的声明如下:
template <typename T>
T* find_name(std::vector<T*> v, std::string name);
Run Code Online (Sandbox Code Playgroud)
当我编译时,我在调用函数时遇到此链接器错误:
Error 1 error LNK2019: unresolved external symbol "class Item * __cdecl find_name<class Item>(class std::vector<class Item *,class std::allocator<class Item *> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$find_name@VItem@@@@YAPAVItem@@V?$vector@PAVItem@@V?$allocator@PAVItem@@@std@@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@@Z) referenced in function "public: class Item * __thiscall Place::get_item(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?get_item@Place@@QAEPAVItem@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) place.obj Program2
Run Code Online (Sandbox Code Playgroud)
同样,我是模板的新手,所以我不知道会发生什么.我通过谷歌找到的LNK2019的所有实例都没有使用正确的库,但由于这是我自己的功能,我不明白为什么会发生这种情况.
另外,一个相关的问题:是否有办法制作模板参数,以便它必须是某个类的子类,即模板?