【发布时间】:2026-02-24 22:05:01
【问题描述】:
目前正在研究由可调整大小的数组控制的堆栈的实现。尝试实例化 ResizableArrayStack 的新对象会出错。
堆栈接口.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
#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
#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;
}
【问题讨论】:
-
那是什么语言?您使用的是哪个编译器?哪些设置?
标签: c++ function class virtual abstract