我有一个基类Collection
,如下所示:
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);
};
在派生类Array
中,我需要实现纯虚拟赋值运算符。在Array.h
中,它被声明为:
public:
virtual Array& operator = (Collection &rhs);// assignment operator
在Array.cpp
中,代码为:
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;
}
编译后,错误是:
未定义对`Collection :: operator =(Collection&amp;)&#39;
的引用
我搜索了相关的网页,通常是因为签名不匹配。但是,我无法弄清楚我的问题。
编辑: 我的main()中产生问题的代码是:
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;