[英]C++ - Object of abstract class type is not allowed
Currently working on an implementation of a stack controlled by a resizable array.目前正在研究由可调整大小的数组控制的堆栈的实现。 Trying to instantiate a new object of
ResizableArrayStack
gives errors.尝试实例化
ResizableArrayStack
的新对象会出错。
E0322 object of abstract class type "csc232::ResizableArrayStack" is not allowed: ResizableArrayStack.cpp 107 function "csc232::ResizableArrayStack::isEmpty [with ItemType=int]" is a pure virtual function function "csc232::ResizableArrayStack::push [with ItemType=int]" is a pure virtual function function "csc232::ResizableArrayStack::pop [with ItemType=int]" is a pure virtual function function "csc232::ResizableArrayStack::peek [with ItemType=int]" is a pure virtual function
StackInterface.h堆栈接口.h
#include "pch.h"
#pragma once
#ifndef CSC232_HW05_RESIZABLE_ARRAY_STACK_STACK_INTERFACE_H
#define CSC232_HW05_RESIZABLE_ARRAY_STACK_STACK_INTERFACE_H
namespace csc232 {
template<typename ItemType>
class StackInterface {
public:
/**
* Sees whether the stack is empty.
* @return True if the stack is empty, or false if not.
*/
virtual bool isEmpty() const = 0;
/**
* Adds a new entry to the top of this stack.
* @param newEntry The object to be added as a new entry.
* @return True if the addition is successful or false if not.
* @post If the operation was successful, newEntry is at the top of the stack.
*/
virtual bool push(const ItemType &newEntry) = 0;
/**
* Removes the top of this stack.
* @return True if the removal was successful or false if not.
* @post If the operation was successful, the top of the stack has been removed.
*/
virtual bool pop() = 0;
/**
* Returns a copy of the top of this stack.
* @return A copy of the top the stack.
* @post A copy of the top of the stack has been returned, and the stack is unchanged.
*/
virtual ItemType peek() const = 0;
/**
* Destroys this stack and frees its assigned memory
*/
virtual ~StackInterface() = default;
};
}
#endif //CSC232_HW05_RESIZABLE_ARRAY_STACK_STACK_INTERFACE_H
ResizableArrayStack.h可调整大小的ArrayStack.h
#include "pch.h"
#pragma once
#ifndef CSC232_HW05_RESIZABLE_ARRAY_STACK_RESIZABLE_ARRAY_STACK_H
#define CSC232_HW05_RESIZABLE_ARRAY_STACK_RESIZABLE_ARRAY_STACK_H
#include "StackInterface.h"
namespace csc232 {
template <typename ItemType>
class ResizableArrayStack : public StackInterface<ItemType> {
private:
ItemType* items;
int top, capacity, count;
static const int DEFAULT_CAPACITY = 10;
public:
ResizableArrayStack();
ResizableArrayStack(int initial_capacity);
ResizableArrayStack(const ResizableArrayStack &rhs);
ResizableArrayStack(ResizableArrayStack &&rhs) = delete;
void operator=(const ResizableArrayStack<ItemType> &rhs);
void operator=(ResizableArrayStack &&rhs) = delete;
~ResizableArrayStack();
bool isEmpty() const = 0;
bool push(const ItemType &newEntry) = 0;
bool pop() = 0;
ItemType peek() const = 0;
int getCapacity() const;
private:
void init();
void increase_size();
};
#endif // CSC232_HW05_RESIZABLE_ARRAY_STACK_RESIZABLE_ARRAY_STACK_H
ResizableArrayStack.cpp ResizableArrayStack.cpp
#include "pch.h"
#include <iostream>
#include "ResizableArrayStack.h"
template<typename ItemType>
csc232::ResizableArrayStack<ItemType>::ResizableArrayStack() : count(0), capacity(DEFAULT_CAPACITY) {
init();
}
template<typename ItemType>
csc232::ResizableArrayStack<ItemType>::ResizableArrayStack(int initial_capacity) : count(0), capacity(initial_capacity) {
init();
}
template<typename ItemType>
void csc232::ResizableArrayStack<ItemType>::init() {
items = new ItemType[capacity];
count = 0;
}
template<typename ItemType>
csc232::ResizableArrayStack<ItemType>::ResizableArrayStack(const ResizableArrayStack &rhs) {
*this = rhs;
}
template<typename ItemType>
void csc232::ResizableArrayStack<ItemType>::operator=(const ResizableArrayStack<ItemType> &rhs) {
if (this != rhs)
{
delete[] items;
init();
for (int i = 0; i < rhs.count; i++)
{
this->push(rhs.items[i]);
}
}
}
template<typename ItemType>
csc232::ResizableArrayStack<ItemType>::~ResizableArrayStack() {
delete[] items;
}
template<typename ItemType>
bool csc232::ResizableArrayStack<ItemType>::isEmpty() const {
return count == 0;
}
template<typename ItemType>
bool csc232::ResizableArrayStack<ItemType>::push(const ItemType &newEntry) {
if (count == capacity)
increase_size();
items[count] = newEntry;
return false;
}
template<typename ItemType>
bool csc232::ResizableArrayStack<ItemType>::pop() {
if (count == 0)
throw std::underflow_error("Underflow exception.");
count--;
return false;
}
template<typename ItemType>
ItemType csc232::ResizableArrayStack<ItemType>::peek() const {
top = capacity - 1;
return items[top];
}
template<typename ItemType>
int csc232::ResizableArrayStack<ItemType>::getCapacity() const {
return capacity;
}
template<typename ItemType>
void csc232::ResizableArrayStack<ItemType>::increase_size() {
capacity = capacity * 2;
ItemType *temp = new ItemType[capacity];
for (int i = 0; i < capacity; i++)
temp[i] = items[i];
delete[] items;
items = temp;
}
int main(int argc, char* argv[]) {
csc232::ResizableArrayStack<int> stack;
}
In your ResizableArrayStack
class definition your virtual function overrides are still "pure":在您的
ResizableArrayStack
类定义中,您的虚拟函数覆盖仍然是“纯”的:
bool isEmpty() const = 0;
bool push(const ItemType &newEntry) = 0;
bool pop() = 0;
ItemType peek() const = 0;
remove the = 0
and for good measure, tell the compiler that these are overrides:删除
= 0
并为了更好的衡量,告诉编译器这些是覆盖:
bool isEmpty() const override;
bool push(const ItemType &newEntry) override;
bool pop() override;
ItemType peek() const override;
A class is abstract if the final overrider of any virtual function is pure virtual.如果任何虚函数的最终覆盖是纯虚的,则一个类是抽象的。 Your definition of template class
ResizableArrayStack
has = 0;
您对模板类
ResizableArrayStack
定义具有= 0;
at the ends of several virtual function overrides.在几个虚函数覆盖的末尾。 This means they are considered pure virtual even though they also have definitions.
这意味着它们被认为是纯虚拟的,即使它们也有定义。 (Defining a pure virtual function is valid C++, and the definitions could be called using a qualified name, but still must be overridden by something else to get a non-abstract class.)
(定义纯虚函数是有效的 C++,可以使用限定名称调用定义,但仍必须由其他内容覆盖以获得非抽象类。)
Just take the = 0
pieces out of ResizableArrayStack
.只需从
ResizableArrayStack
取出= 0
块即可。
But note also, you normally should not put definitions with template parameters in a *.cpp file: See the Q&A "Why can templates only be implemented in the header file?"但还要注意,通常不应将带有模板参数的定义放在 *.cpp 文件中:请参阅问答“为什么模板只能在头文件中实现?”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.