简体   繁体   English

C ++中的抽象类和纯虚方法

[英]Abstract Class and Pure Virtual Method in C++

I have 4 C++ files, 2 headers, and 2 .cc files. 我有4个C ++文件,2个头文件和2个.cc文件。 This is just a proof of concept but I can't seem to get it right. 这只是概念的证明,但我似乎无法正确理解。

My first header looks like this: 我的第一个标题看起来像这样:

#ifndef INT_LIST_H
#define INT_LIST_H
class IntList
{
  public:

     //Adds item to the end of the list
     virtual void pushBack(int item) = 0;
};
#endif

My second header uses the first and looks like this: 我的第二个标头使用第一个标头,如下所示:

#ifndef ArrayIntList_H
#define ArrayIntList_H
#include "IntList.h"


class ArrayIntList : public IntList
{
    private:
        int* arrayList;
        int* arrayLength;

     public:
        //Initializes the list with the given capacity and length 0
        ArrayIntList(int capacity);

        //Adds item to the end of the list
        virtual void pushBack(int item) = 0;

};
#endif  

my first .cc file fills in the methods of the previous class: 我的第一个.cc文件填充了上一类的方法:

#include <iostream>
#include "ArrayIntList.h"

ArrayIntList::ArrayIntList(int capacity)
{
    //make an array on the heap with size capacity
    arrayList = new int[capacity];
    //and length 0
    arrayLength = 0; 
}

void ArrayIntList::pushBack(int item)
{
    arrayList[*arrayLength] = item;
}

And this is my main function: 这是我的主要功能:

#include <iostream>
#include "ArrayIntList.h"

int main(int argc, const char * argv[])
{
    ArrayIntList s(5);
}

When I run this in Xcode I get an error that "Variable ArrayIntList is an abstract class" I don't understand how this can be since I defined it in my above .cc file. 当我在Xcode中运行此命令时,出现错误消息“ Variable ArrayIntList是一个抽象类”,因为我在上面的.cc文件中定义了它,所以我不明白该怎么办。 Any ideas? 有任何想法吗?

On class ArrayIntList use this 在类ArrayIntList上使用此

virtual void pushBack(int item);

instead of this 代替这个

virtual void pushBack(int item) = 0;

The reason is that when you assign a 0 to a function declaration, you are saying it is "pure", or not implemented. 原因是当您将0分配给函数声明时,就表示它是“纯”的或未实现。 But you are doing that (implementing it) below. 但是您正在执行以下操作(实现)。

You've got ArrayIntList::pushBack(int item) declared as a pure virtual function. 您已经将ArrayIntList::pushBack(int item)声明为纯虚函数。 That's what that = 0 does. 那就是= 0 Remove the = 0 from ArrayIntList.h. 从ArrayIntList.h中删除= 0

Also: you're using an int pointer instead of an int to track your array length. 另外:您使用的是int指针而不是int来跟踪数组长度。

In your declaration of the ArrayIntList class, you need to remove the "= 0" from the method declaration. 在ArrayIntList类的声明中,您需要从方法声明中删除“ = 0”。 You probably also need to declare arrayLength to be an int rather than a pointer to an int. 您可能还需要声明arrayLength为一个int而不是一个指向int的指针。 Finally, since you're allocating memory for the array in your constructor, you should declare a destructor to free the memory when the object is destroyed: 最后,由于要在构造函数中为数组分配内存,因此应在对象销毁时声明一个析构函数以释放内存:

class ArrayIntList : public IntList
{
private:
    int* arrayList;
    int arrayLength;

public:
    //Initializes the list with the given capacity and length 0
    ArrayIntList(int capacity);

    virtual ~ArrayIntList() { delete arrayList; }

    //Adds item to the end of the list
    virtual void pushBack(int item);

};

Of course, the best way to handle the array list would be to use a std::vector<int> instead so you don't have to manually handle the memory allocation and deallocation 当然,处理数组列表的最佳方法是使用std::vector<int>因此您不必手动处理内存分配和释放

In class ArrayIntList you are declaring a pure-virtual "virtual void pushBack(int item) = 0;" 在ArrayIntList类中,您声明了一个纯虚拟的“虚拟void pushBack(int item)= 0;”。 which you have already declared in the abstract parent IntList. 您已经在抽象父IntList中声明了它。 All you need to do is declare it as "virtual void pushBack(int item);". 您需要做的就是将其声明为“ virtual void pushBack(int item);”。

An abstract base class cannot inherit from another Abstract base class, remove the 一个抽象基类不能从另一个Abstract基类继承,删除

= 0;

from your equation in ArrayIntList: 从您在ArrayIntList中的方程式:

virtual void pushBack(int item) = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM