嗨,我正在尝试实现一个基于数组的堆栈(在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> ©); // 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> ©) {
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也是如此,因此只有
pop
和initialize_stack
在代码中保留原始签名。关于c++ - 抽象类和虚拟方法的问题:无法声明变量“list”为抽象类型“aStack <int>” aStack <int> list(5),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39467903/