Template issue causes linker error (C++)(模板问题导致链接器错误 (C++))
问题描述
我对 C++ 模板的情况知之甚少,但我正在尝试实现一个函数,该函数在向量中搜索满足给定属性的元素(在本例中,搜索具有给定名称的元素).我在 .h 文件中的声明如下:
I have very little idea what's going in regards to C++ templates, but I'm trying to implement a function that searches a vector for an element satisfying a given property (in this case, searching for one with the name given). My declaration in my .h file is as follows:
template <typename T>
T* find_name(std::vector<T*> v, std::string name);
当我编译时,调用函数时出现此链接器错误:
When I compile, I get this linker error when I call the function:
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@@@@V?$basic_string@DU?$char_traits@D@V?$allocator@D@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@V?$allocator@D@@Z) place.obj Program2
同样,我是模板的新手,所以我不知道发生了什么.我通过 Google 发现的所有 LNK2019 实例都是关于没有使用正确的库,但由于这是我自己的函数,我不明白为什么会发生这种情况.
Again, I'm new to templates so I don't know what's going. All instances I've found of LNK2019 through Google have been about not using the correct libraries, but since this is my own function I don't see why this would be happening.
另外,一个相关的问题:有没有办法让模板参数成为某个类的子类,即模板?
Also, a related question: Is there a way to make a template parameter so that it has to be a subclass of a certain class, i.e. template?
推荐答案
您必须在调用站点上提供您的模板定义.这意味着没有 .cpp
文件.
You have to have your template definitions available at the calling site. That means no .cpp
files.
原因是模板无法编译.把函数想象成饼干,而编译器就是一个烤箱.
The reason is templates cannot be compiled. Think of functions as cookies, and the compiler is an oven.
模板只是一个曲奇饼,因为它们不知道它们是什么类型的曲奇饼.它只告诉编译器在给定类型时如何制作函数,但它本身无法使用,因为没有具体的类型被操作.你不能做饼干刀.只有当你准备好美味的饼干面团时(即,给编译器面团 [类型]))你才能切割饼干并烹饪它.
Templates are only a cookie cutter, because they don't know what type of cookie they are. It only tells the compiler how to make the function when given a type, but in itself, it can't be used because there is no concrete type being operated on. You can't cook a cookie cutter. Only when you have the tasty cookie dough ready (i.e., given the compiler the dough [type])) can you cut the cookie and cook it.
同样,只有当你实际使用某个类型的模板时,编译器才能生成实际的函数,并编译它.但是,如果缺少模板定义,则无法执行此操作.你必须把它移动到头文件中,这样函数的调用者才能制作cookie.
Likewise, only when you actually use the template with a certain type can the compiler generate the actual function, and compile it. It can't do this, however, if the template definition is missing. You have to move it into the header file, so the caller of the function can make the cookie.
这篇关于模板问题导致链接器错误 (C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:模板问题导致链接器错误 (C++)
基础教程推荐
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01