2010-02-21 60 views
1

我有一个类型节点列表。我想设置一个临时的节点在列表的前面等于节点,如下所示:涉及运算符过载函数的C++链接器错误

class Node 
{ 
    public: 
     Node(); 
     Node& operator = (const Node& n); 
}; 

,但我不断收到一个链接错误:

Linking...
main.obj : error LNK2019: unresolved external symbol "public: class Node & __thiscall Node::operator=(class Node const &)" ([email protected]@[email protected]@@Z) referenced in function "void __cdecl fillScan(int,class std::list >)" ([email protected]@[email protected]@@[email protected]@@@[email protected]@@[email protected]@@Z)
C:\Users\Aaron McKellar\Documents\School Stuff\CS445\Test\Debug\Test.exe : fatal error LNK1120: 1 unresolved externals

提前感谢!

+0

感谢您的回复。我有一段时间没有做过运算符重载。即使浏览了我的书和在线,我仍然遇到了麻烦。 class节点 { public: Node(); int y; 节点运算符=(const的节点& n); }; 节点::节点()//默认构造 { Y = -1;} 节点&节点::运算符=(const的节点&N) { 如果( !这= N) { this.y = NY; } 回报*本; } 我不知道什么是错的,但是当我做this.y IntelliSense不认识到这一点作为一个节点对象。Please help and thanks! – 2010-02-22 00:22:54

+0

1>编译... 1> main.cpp 1> c:\ us ers \ aaron mckellar \ documents \ school stuff \ cs445 \ test \ test \ main.cpp(49):error C2679:binary'!=':没有找到操作符,它需要'const Node'类型的右手操作数没有可接受的转换) 1>可能是'内置C++操作符!=(Node *,Node *)' 1> c:\ program files \ microsoft sdks \ windows \ v6.0a \ include \ guiddef.h (197):或'int operator!=(const GUID&,const GUID&)' 1>尝试匹配参数列表'(Node * const,const Node)' – 2010-02-22 00:23:48

+0

1> c:\ users \ aaron mckellar \ documents \ school stuff \ cs445 \ test \ test \ main.cpp(51):error C2228:'.y'的左边必须有class/struct/union 1> type是'Node * const' 1>打算用' - >'来代替? – 2010-02-22 00:24:21

回答

2

您只显示operator=的声明,而不是定义。要么你没有提供定义,要么链接器找不到它。

嗯,我应该说:链接器绝对找不到operator=的定义。要么是因为你忘了提供一个,要么是因为你的项目/ Makefile设置不正确。

+0

请查看我的意见以上 – 2010-02-22 00:35:20

0

您需要提供一个定义operator=,当然:

Node& Node::operator=(const Node& n) { 

    // 'copy' semantics for Node 
} 

注意,编译器使用。如果没有提供按成员拷贝自己生成的赋值运算符。足够的话,使用编译器生成的运算符。

+0

请查看我的意见以上 – 2010-02-22 00:35:50