错误LNK2019是什么意思
有人可以告诉我以下错误意味着什么吗?
错误 2 错误 LNK2019:未解决 外部符号“public:类 TLst & __thiscall TLst::运算符=(类 TLst常量&)" (??4?$TLst@VTInt@@@@QAEAAV0@ABV0@@Z) 在函数“public:void __thiscall TPair >::GetVal(类 TInt &,class TLst &)const " (?GetVal@?$TPair@VTInt@@V?$TLst@VTInt@@@@@@QBEXAAVTInt@@AAV?$TLst@VTInt@@@@@Z) randomgraph.obj randomgraph
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,这意味着链接器看到了对符号的引用,但在任何地方都找不到它——通常是由于缺少库或目标文件。
在这种情况中,发生这种情况是因为您在 .cpp 文件中实现了模板化类的成员函数 - 它们应该在标头中实现。
模板类是一个模板,而不是一个类。当编译器看到您使用例如
vector 时, f;
它从模板vector
创建一个新类vector
。为了创建例如vector::size()
,它需要在模板实例化时查看size()
的实现 - 并且它可以如果头文件中没有size()
的实现,则不要这样做。您可以通过显式实例化
int
的vector
来解决此问题 - 然后编译器在编译 cpp 文件时将看到显式实例化。这违背了拥有模板的目的 - 它只能用于您通过显式实例化预定义的类型。因此,总而言之,始终在头文件中完全实现模板。In general, it means that the linker sees a reference to a symbol, but it can't find it anywhere - often due to a missing library or object file.
In this case this happened because you implemented your templated class'es member functions in a .cpp file - they should be implemented in the header.
A template class is a template not a class. When the compiler see you using e.g.
vector<int> f;
it creates a new classvector<int>
from the templatevector
. In order to create e.g.vector<int>::size()
it needs to see the implementation ofsize()
at the point where the template is instantiated - and it can't do that if the implementation ofsize()
isn't in the header file.You can get around this by explicitly instantiating
vector
forint
- Then the compiler will see the explicit instantiation when it compiles the cpp file. This defeats the purpose of having a template - it'd only be usable for the types you predefine with explicit instantiation. So, short story, always fully implement templates in header files.未解析的外部符号
意味着链接器找不到引用。这通常是由于忘记将目标文件或库添加到链接步骤而引起的。 (包含类的头文件是不够的 - 您还必须添加实现代码。)Unresolved external symbol
means that there's a reference that the linker can't find. It's usually caused by forgetting to add an object file or library to the link step. (Including the header file for a class isn't enough - you also have to add the implementation code.)这个问题就解决了。在模板类 TLst 中,函数
TLst TLst::operator=(const TLst&);
已声明但未定义。
我必须在 .cpp 文件中定义该函数。我也可以在我的头文件中定义它。
感谢您的回复。
索姆纳特
This problem is solved. In the template class TLst, the function
TLst TLst::operator=(const TLst&);
was declared but it was not defined.
I had to define the function in my .cpp file. I could have defined it in my header file as well.
Thanks for the replies.
Somnath