【问题标题】:How is this derived class abstract?这个派生类是如何抽象的?
【发布时间】:2012-12-08 08:16:47
【问题描述】:

所以我有一个抽象基类,Collection。我理解它是抽象的,因为它至少声明了一个纯虚函数。

我有一个 Collection 的子类,OrderedCollection,它完全在它自己的头文件中声明了这些函数。然后我在 OrderedCollection 的源文件中定义了完全相同的函数。

代码如下:

类集合.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 文件并更新了我针对函数参数不匹配所做的一些修复。

【问题讨论】:

  • 你在Collection.cpp中实现了哪些Collection方法?
  • 现在用 .cpp 文件更新帖子。还修复了一些参数不匹配的问题。
  • 你没有实现Collection&amp; copy()方法,看我的回答。
  • 您是否真的发布了您的实际代码以及整个编译器错误?因为这段代码比其中的错误多得多,并且当 VS 报告抽象类实例化的问题时,它通常会准确地告诉您您没有覆盖的内容。
  • 它给了你这个错误,因为这个类仍然是抽象的。您还没有覆盖所有纯虚拟,因为您将参数类型更改为至少其中之一。

标签: c++ visual-studio-2010 virtual abstract-class


【解决方案1】:
virtual Collection& operator=(const Collection& other)=0;

在子类中没有被覆盖,因为:

OrderedCollection& operator=(OrderedCollection& other);

有不同的签名。

不同之处不仅在于“const”,还在于实际类型。被覆盖的类也必须采用 Collection,而不是派生的 OrderedCollection。

【讨论】:

  • 是的,我其实只是注意到了这一点并进行了更改,但仍然存在问题。
  • 确切的错误是,错误:抽象类类型“OrderedCollection”的对象是不允许的...这是当我尝试将指针对象分配给此副本中的 OrderedCollection 的新实例时( ) 方法在底部。
  • 我已经更新了我的答案,因为你还没有完全纠正这个问题。
  • 修复签名中的const 无济于事,因为赋值运算符不会覆盖基类的虚拟运算符(因为它仍然具有不同的签名)。所以派生类仍然是抽象的。问题是——为什么基类中有一个虚拟运算符?
  • @BoPersson 我说他需要修复 const 和 type。如果你这样做,它会覆盖基类并停止抽象类。
猜你喜欢
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 2018-04-17
  • 2014-11-16
  • 2011-02-01
相关资源
最近更新 更多