模板问题导致链接器错误 (C++)

Template issue causes linker error (C++)(模板问题导致链接器错误 (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++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Could I ever want to access the address zero?(我可以想访问地址零吗?)
C++ Access derived class member from base class pointer(C++ 从基类指针访问派生类成员)
Weird Behaviour with const_cast(const_cast 的奇怪行为)
Are pointer variables just integers with some operators or are they quot;symbolicquot;?(指针变量只是带有某些运算符的整数还是“符号?)
Modifying a char *const string(修改 char *const 字符串)
Modifying a const int in C++(修改 C++ 中的 const int)