因此,我有一个抽象基类Collection。我了解它是抽象的,因为它声明了至少一个纯虚函数。
我有Collection的子类OrderedCollection,该子类恰好在其自己的头文件中声明了这些函数。然后,我在OrderedCollection的Source文件中定义那些完全相同的函数。
这是代码:
类Collection.h
class Collection {
public:
virtual Collection& add(int i, int index) = 0; //pure virtual
virtual Collection& remove(int i) = 0; //pure virtual
virtual Collection& operator=(const Collection& other)=0; //pure virtual
virtual int& operator[](int i) = 0; //pure virtual
Collection& iterate(void (*pFunc)()); //Function takes pointer to function as argument
bool contains(int i);
virtual Collection& copy();
virtual ~Collection();
int size() { return size; }
int capacity() { return capacity; }
//virtual void swap(Collection& other);
protected:
Collection(){}
std::vector collectionVec;
int size;
int capacity;
};
派生类OrderedCollection.h:
class OrderedCollection : public Collection
{
public:
OrderedCollection& add(int i, int index);
OrderedCollection& remove(int i);
OrderedCollection& operator=(const OrderedCollection& other);
int& operator[](int i);
OrderedCollection();
//OrderedCollection::OrderedCollection(int pFirst, int pLast, int pSize, int pCapacity, std::vector<int> passedVec);
//OrderedCollection(const OrderedCollection& other); //Copy constructor
virtual ~OrderedCollection();
virtual OrderedCollection& copy(OrderedCollection& passedCollection);
protected:
//int* first;
//int* last;
int first;
int last;
OrderedCollection& grow(); //Utility Function
};
aaand OrderedCollection.cpp:
#include "OrderedCollection.h"
OrderedCollection& OrderedCollection::add(int i, int index){
if(size == capacity){ //If vector is full
}
return *this;
}
OrderedCollection& OrderedCollection::remove(int i){
if(first <= last){
for(int j = first; j <= last; j++){
if(collectionVec.at(j) == i){
collectionVec.erase(j);
last--;
}
}
}
/*for(int j = 0; j < collectionVec.size(); j++){
if(collectionVec.at(j) == i)
} */
return *this;
}
OrderedCollection& OrderedCollection::operator=(const OrderedCollection& other){
if (this != &other) // protect against invalid self-assignment
{
// 1: allocate new memory and copy the elements
std::vector<int> *new_vector = new std::vector<int>(other.capacity);
std::copy(other.collectionVec, other.collectionVec + other.capacity, new_vector);
// 2: deallocate old memory
collectionVec.clear();
// 3: assign the new memory to the object
collectionVec = *new_vector;
size = other.size; //wtf
capacity = other.capacity; //wtf
delete new_vector;
}
// by convention, always return *this
return *this;
}
int& OrderedCollection::operator[](int i){ //is return type correct? It makes more sense to have a return type of int or int&, right?
int temp = 0;
if(first <= last){
if(collectionVec.at(first + i) != NULL){ //Need to redo this
return collectionVec.at(first + i);
}
}
return temp;
}
OrderedCollection::OrderedCollection() : Collection()
{
//first = &collectionVec.at(2);
//last = &collectionVec.at(1);
//Crossed at construction
first = 2;
last = 1;
}
OrderedCollection::~OrderedCollection(void)
{
//first = NULL;
//last = NULL;
collectionVec.clear();
}
OrderedCollection& OrderedCollection::grow(){
int newFirst = capacity / 2;
std::vector<int> *new_vector = new std::vector<int>(2 * capacity);
std::copy(collectionVec, collectionVec+size, new_vector->begin() + newFirst); //Want to return iterator pointing to
collectionVec = new_vector;
first = newFirst;
last = first + size;
capacity = collectionVec.size;
delete new_vector;
return *this;
}
OrderedCollection& OrderedCollection::copy(OrderedCollection& passedCollection){
OrderedCollection* temp = new OrderedCollection() //ERROR is here. VS highlights constructor method
return *this;
}
现在,当我试图在此最后一个copy()中创建类型为OrderedCollection的值标识符时,问题就来了。据我了解,如果类是抽象的,则不应允许我这样做(显然,它是抽象的,VS也告诉我)。但是还有另一个问题。当我尝试创建一个新的OrderedCollection对象并将其分配给temp时,出现相同的错误。上面的初始化根据VS是很好的(IDE不会抱怨,尽管它对我没有帮助)。但是我不知道为什么将此类视为抽象。
如果我错了,请纠正我,但这应涵盖所有基础,以确保此派生类不是抽象的。
错误的确切原因是:错误:不允许抽象类类型为“OrderedCollection”的对象...这是当我尝试在底部的copy()方法中将指针对象分配给OrderedCollection的新实例时。
让我在下面发布我的Collection.cpp文件:
#include "Collection.h"
Collection::Collection()
{
size = 0;
capacity = 4;
collectionVec.resize(capacity);
}
Collection::~Collection()
{
}
Collection& Collection::iterate(void (*pFunc)()){
return *this;
}
bool contains(int i){
return true;
}
编辑:添加了Collection.cpp文件,并更新了我对不匹配的函数参数所做的一些修复。
最佳答案
virtual Collection& operator=(const Collection& other)=0;
不会在子类中被覆盖,因为:
OrderedCollection& operator=(OrderedCollection& other);
具有不同的签名。
不同之处不仅在于“const”,还在于实际类型。重写的类还必须具有Collection,而不是派生的OrderedCollection。