だから私は抽象的な基本クラス、コレクションを持っています。少なくとも1つの純粋仮想関数を宣言しているため、抽象的であると理解しています。
CollectionのサブクラスであるOrderedCollectionがあります。これは、それらの関数を独自のヘッダーファイルで正確に宣言します。次に、これらのまったく同じ関数をOrderedCollectionのソースファイルで定義します。
コードは次のとおりです。
クラス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ファイルを追加し、関数パラメーターの不一致に関して行ったいくつかの修正を更新しました。