I have a base class Collection
as follows:
class Collection {
protected:
int size_;
public:
//virtual size()
virtual int size() = 0;
//virtual destructor
virtual ~Collection();
//copy constructor for deep copy
Collection();
//virtual copy constructor
Collection(const Collection& set);
//virtual method copy()
virtual Collection* copy()=0;
//virtual operator[]
virtual int& operator[](int pos) = 0;
//virtual operator=
virtual Collection& operator=(Collection &rhs) = 0;
//virtual add()
virtual Collection& add(int number) = 0;
bool contains(int i);
};
In a derived class Array
, I need to implement the pure virtual assignment operator. In Array.h
, it is declared as:
public:
virtual Array& operator = (Collection &rhs);// assignment operator
In Array.cpp
, the code is:
Array& Array::operator=(Collection &rhs)
{
Array pArr = dynamic_cast<Array&>(rhs);
if(this == &pArr)
{
return *this;
} // handling of self assignment.
delete[] pa; // freeing previously used memory
pa = new int[pArr.size_];
size_ = pArr.size_;
memcpy(pa, pArr.pa, sizeof(int) * size_);
return *this;
}
After compiling, the error is:
undefined reference to `Collection::operator=(Collection&)'
I searched related webpages, usually it is because of the signatures are not matching. However, I could not figure out my problem here.
EDIT: The code in my main() that generates the problem is:
Array a1; // create an array object, size 10
Array a2; // create an array object, size 10
a1.add(1); // add data to array
a1.add(2);
a1.add(3);
a1[3] = 4;
a2.add(4);
a2 = a1;