模板问题导致链接器错误(C ++) [英] Template issue causes linker error (C++)

查看:113
本文介绍了模板问题导致链接器错误(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@@@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

再次,我是模板的新手,所以我不知道发生了什么。通过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 ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆