When I try to overload the copy constructor, it gives me the following error code.
I encountered similar error message when I was trying to overload the << operator.
I fixed it by defining inside header
However, this time the same trick does not work..
How do I fix this and What is really going on to cause this error message?
Here is my copy constructor definition
template <class ItemType>
void List342<ItemType>::operator=(List342 &source)
{
Node *sNode, *dNode, *insNode;
if (this == &source) return;
this->Clear();
if (source.head == NULL)
{
return;
}
dNode = new Node;
dNode->value = (source.head)->value;
this->head = dNode;
sNode = (source.head)->next;
while (sNode != NULL)
{
insNode = new Node;
insNode->value = sNode->value;
dNode->next = insNode;
dNode = dNode->next;
sNode = sNode->next;
}
}
And here is my header file.
#ifndef LIST342_H_
#define LIST342_H_
#include <iostream>
#include <string>
using namespace std;
template <class ItemType>
class List342 {
friend ostream& operator<<(ostream &outStream, const List342 &list)
{
Node *pNode;
pNode = list.head;
while (pNode != NULL)
{
outStream << *pNode->data;
pNode = pNode->next;
}
return outStream;
}
public:
List342();
List342(List342<ItemType> &);
~List342();
bool BuildList(string FileName);
bool Insert(ItemType *obj);
bool Remove(ItemType target, ItemType &result);//check
bool Peek(ItemType target, ItemType &result);//check
bool isEmpty();
void ClearList();
bool Merge(List342 &list1, List342 &list2);
List342 operator+(const List342 &list) const;
List342 operator+=(const List342 &list);
bool operator==(const List342 &list) const;
bool operator!=(const List342 &list) const;
void operator=(List342 &);
private:
struct Node {
ItemType *data;
Node *next;
};
Node *head;
};
#endif /* LIST342_H_ */