1
votes

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!

2
Thanks for the replies. I have not done operator overloading in some time. I am still having trouble with this even after looking through my book and online. class Node { public: Node(); int y; Node& operator=(const Node& n); }; Node::Node() // Default constructor { y = -1; } Node& Node::operator=(const Node& n) { if(this != n) { this.y = n.y; } return *this; } I don't know what is wrong but when I do this.y intellisense does not recognize this as being a Node object. Please help and thanks!Aaron McKellar
1>Compiling... 1>main.cpp 1>c:\users\aaron mckellar\documents\school stuff\cs445\test\test\main.cpp(49) : error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'const Node' (or there is no acceptable conversion) 1> could be 'built-in C++ operator!=(Node *, Node *)' 1> c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(197): or 'int operator !=(const GUID &,const GUID &)' 1> while trying to match the argument list '(Node *const , const Node)'Aaron McKellar
1>c:\users\aaron mckellar\documents\school stuff\cs445\test\test\main.cpp(51) : error C2228: left of '.y' must have class/struct/union 1> type is 'Node *const ' 1> did you intend to use '->' instead?Aaron McKellar
I also tried return this = n; but I got this: 1>c:\users\aaron mckellar\documents\school stuff\cs445\test\test\main.cpp(56) : error C2440: '=' : cannot convert from 'const Node' to 'Node *const ' 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be calledAaron McKellar

2 Answers

3
votes

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.

0
votes

You need to supply a definition for operator=, of course:

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

     // 'copy' semantics for Node
}

Note that the compiler generates the assignment operator by itself using memberwise copy if none is provided. Use the compiler-generated operator if sufficient.