c++ - 抽象类和虚方法问题 : cannot declare variable ‘list’ to be of abstract type ‘aStack<int>’ aStack<int> list(5)

标签 c++ polymorphism

您好,我正在尝试实现一个基于数组的堆栈(在 C++ 中),但我已经坚持了一段时间。每当我尝试在源文件中创建对象时,编译器都会生成错误“无法将变量‘list’声明为抽象类型‘aStack’ aStack list(5);"尽管我的 aStack 类继承自 ADT abs_stack。这是我的代码

enter code here

    // question #1. 
    // ADT for stack.
    template <class type> 
    class abs_stack {
    public:
    virtual void initialize_stack() = 0; // starts the stack.  
    virtual bool isEmpty() = 0; // checks whether stack is empty or not.
    virtual bool isFull() = 0; // checks whether stack is full or not. 
    virtual void push(type n) = 0; // 
    virtual void pop() = 0; 
    virtual type top() = 0; 
    };

    // declaring array based stack. 
    template <typename t> 
    //template <typename E> 
   // class AStack: public Stack<E>
   class aStack : public abs_stack<t> {
    private:
    t *grid; // stack. 
    int max_stack_size; 
    int top_element; // points to the top of the stack.  
    void stack_copy(aStack<t> &copy); // used in copy contructor. 
    public: 
    aStack(int size); // paratmeterized constructor.
    ~aStack(); // destructor. 
    aStack(const aStack <t> &); // copy constructor. 
    const aStack<t> & operator = 
    (const aStack<t> &originalStack); // overloaded assignment operator. 
    //------------------------------------->
    bool isEmpty() const; 
    bool isFull() const; 
    void pop(); 
    void push(const t &newElem); 
    t top() const; 
    void initialize_stack(); 
     };

enter code here

    #ifndef H_stack
    #define H_Stack

    #include <iostream>
    #include <cassert>
    #include "stack.h"
    using namespace std;

    // stack methods' implementation.

    template <class x> const aStack<x>& aStack<x>::operator=
                                (const aStack<x>& otherStack)
    {
    if (this != &otherStack) //avoid self-copy
    copyStack(otherStack);
    return *this;
    }//   end of overloaded assignment operator. 

    template <typename x> 
    void aStack<x> :: pop() {

    if (!isEmpty()) {
    top_element--; // decreases the position # of the top element. 
    }
    else {
    cout << "Error! Stack is already. Cannot remove anything." << endl; 
    }
    } // end of pop() 

    template <typename x>
    void aStack<x> :: push(const x &newElem) {
    if (!isFull()) {
    grid[top_element] = newElem; // places new element on the top
    top_element++;
    }
   else {
    cout << "No space available." << endl; 
    }
    } // end of push

    template <typename x> 
    x aStack<x> :: top() const {
    assert(top_element != 0); // checks whether stack exists or not. 
    return grid[top_element - 1]; 
    } 

    template <typename x> 
    void aStack<x>::initialize_stack() {
    top_element = 0; 
    } 

    template <typename x>
    bool aStack<x> :: isEmpty() const {
    return (max_stack_size == 0);
    }

    template <typename x> 
    bool aStack<x> :: isFull() const {
    return (top_element == max_stack_size);
    }

    template <typename x>
    aStack<x> :: aStack(int size) {
    if ( size <= 0) {
    cout << "Error! Stack size can" << 
    " never be lesser than/equal to 0." <<   endl
    << "Generating stack of size 10" << endl; 
    max_stack_size = 10; 
    } 
    else {
    max_stack_size = size; 
    }
    top_element = 0; 
    grid = new x[max_stack_size];
    } // end of parameterized contructor. 

    template <typename x> 
    aStack<x> :: ~aStack() {
    delete []grid; 
    grid = NULL; 
    max_stack_size = 0; 
    } // end of destructor. 

    template <typename x> 
    aStack<x> :: aStack(const aStack<x> &firstStack) {
    grid = NULL;
    stack_copy(firstStack); 
    } // end of copy contructor. 

    template <typename x> 
    void aStack<x> :: stack_copy(aStack<x> &copy) {
    delete []grid; 
    grid = NULL; 
    max_stack_size = copy.max_stack.size; 
    top_element = copy.top.element; 
    grid = new x[max_stack_size]; 
    // generating deep copy of the original stack. 
    for (int i =0; i < max_stack_size; i ++)
    grid[i] = copy.grid[i]; 
   }    // end of stack copy.
   #endif

   #include <iostream>
   #include "stack.h"
   using namespace std; 
   //--------- source file-->
   int main() {
   aStack<int> list(5);
   return 0;

   }

最佳答案

该错误表明您的类 aStack 是一个抽象类,即至少有一个纯虚方法,因此无法实例化。这意味着您的一些 aStack 方法由于签名不匹配而未能参与虚拟覆盖。

请注意,cv-qualifiers 和 ref-qualifires 会影响参数类型并更改签名:

void f(int x)
void f(const int& x)

被认为是不同的功能。成员函数 cv-qualifiers 也是如此,因此只有 popinitialize_stack 保留代码中的原始签名。

关于c++ - 抽象类和虚方法问题 : cannot declare variable ‘list’ to be of abstract type ‘aStack<int>’ aStack<int> list(5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467903/

相关文章:

c++ - 使用 OpenMP 的并行执行比串行执行 C++ 花费的时间更长,我是否以正确的方式计算执行时间?

c++ - 对其中包含类项的 vector 进行排序

c++ - 如何遍历字符串中的所有 utf-8 代码点

c++ - 套接字:我的 select() 函数逻辑具有未定义的行为

c++ - 没有匹配的成员函数调用 'push_back' ,共享指针 vector

haskell - 如何自动将 Data.Bits 添加到 Data.Modular?

scala - 为什么编译器不能推断包对象的类型参数(虽然它对对象工作正常)?

java - 是否可以通过Hashmap制裁剪品(武器/盔甲)的装备(键是 body 的一部分,值是盔甲或武器(如果是手)

C++ 抽象类作为 std::map 键

python - Python 中的类、方法和多态性