C++涉及运算符重载函数的链接器错误
我有一个 Node 类型的列表。我想设置一个临时节点等于列表前面的节点,如下所示:
class Node
{
public:
Node();
Node& operator = (const Node& n);
};
但我不断收到链接器错误:
正在链接...
main.obj :错误 LNK2019:无法解析的外部符号“public:class Node & __thiscall Node::operator=(class Node const &)”(??4Node@@QAEAAV0@ABV0@@Z) 在函数“void __cdecl”中引用fillScan(int,class std::list >)" (?fillScan@@YAXHV?$list@VNode@@V?$allocator@VNode@@@std@@@std@@@Z)
C:\Users\Aaron McKellar\Documents\School Stuff\CS445\Test\Debug\Test.exe:致命错误 LNK1120:1 个无法解析的外部
提前致谢!
I have a List of type Node. I want to set a temporary Node equal to the Node at the front of the List, as follows:
class Node
{
public:
Node();
Node& operator = (const Node& n);
};
but I keep getting a Linker Error:
Linking...
main.obj : error LNK2019: unresolved external symbol "public: class Node & __thiscall Node::operator=(class Node const &)" (??4Node@@QAEAAV0@ABV0@@Z) referenced in function "void __cdecl fillScan(int,class std::list >)" (?fillScan@@YAXHV?$list@VNode@@V?$allocator@VNode@@@std@@@std@@@Z)
C:\Users\Aaron McKellar\Documents\School Stuff\CS445\Test\Debug\Test.exe : fatal error LNK1120: 1 unresolved externals
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只显示了
operator=
的声明,而不是定义。要么您没有提供定义,要么链接器找不到它。好吧,我应该说:链接器肯定找不到
operator=
的定义。这要么是因为您忘记提供一个,要么是因为您的项目/Makefile 设置不正确。You only showed the declaration of
operator=
, not the definition. Either you didn't supply a definition or the linker can't find it.Well, I should say: The linker definitely can't find the definition for
operator=
. Either that's because you forgot to supply one or because your project/Makefile is set up incorrectly.当然,您需要提供
operator=
的定义:请注意,如果未提供,编译器会使用成员复制自行生成赋值运算符。如果足够,请使用编译器生成的运算符。
You need to supply a definition for
operator=
, of course:Note that the compiler generates the assignment operator by itself using memberwise copy if none is provided. Use the compiler-generated operator if sufficient.