C++抽象类实例化报错:无法创建ResizableArrayStack对象
csc232::ResizableArrayStack Hey, let's break down why you're seeing that E0322 error and fix it step by step. The core issue here is that even though you've implemented the stack methods in your .cpp file, your ResizableArrayStack class is still marked as abstract—because you accidentally re-declared the inherited pure virtual functions as pure virtual again in the header.
What's Causing the Error?
Look at your ResizableArrayStack.h file:
bool isEmpty() const = 0; bool push(const ItemType &newEntry) = 0; bool pop() = 0; ItemType peek() const = 0;
Adding =0 to these function declarations tells the compiler "this function is still unimplemented, so this class is abstract." Even though you wrote code for them in .cpp, the header is what the compiler checks when you try to create an instance—so it blocks you from instantiating an abstract class.
Step-by-Step Fixes
1. Update the Header File
First, remove the =0 from those four function declarations in ResizableArrayStack.h. That tells the compiler you're providing concrete implementations for these inherited methods:
// Remove the =0 from each of these: bool isEmpty() const; bool push(const ItemType &newEntry); bool pop(); ItemType peek() const;
2. Fix Logic Bugs in the Implementation
Your .cpp file has a few logic issues that need fixing too:
pushandpopalways returnfalse(which contradicts their purpose)peekis returning the last element of the array, not the actual top of the stack- The copy assignment operator has a pointer comparison bug
increase_sizeis copying more elements than exist in the stack
Here's the corrected .cpp code for those key parts:
template<typename ItemType> bool csc232::ResizableArrayStack<ItemType>::push(const ItemType &newEntry) { if (count == capacity) increase_size(); items[count] = newEntry; count++; // Don't forget to increment the element count! return true; // Return true on success } template<typename ItemType> bool csc232::ResizableArrayStack<ItemType>::pop() { if (isEmpty()) { // You can throw an error or return false—just be consistent with your interface throw std::underflow_error("Underflow exception."); // Or return false; } count--; return true; // Return true on success } template<typename ItemType> ItemType csc232::ResizableArrayStack<ItemType>::peek() const { if (isEmpty()) throw std::underflow_error("Underflow exception."); return items[count - 1]; // Stack top is at count-1, not capacity-1 } template<typename ItemType> void csc232::ResizableArrayStack<ItemType>::operator=(const ResizableArrayStack<ItemType> &rhs) { if (this != &rhs) { // Fix: compare pointers, not objects delete[] items; capacity = rhs.capacity; init(); count = rhs.count; // Direct copy instead of calling push (avoids unnecessary resizing) for (int i = 0; i < rhs.count; i++) { items[i] = rhs.items[i]; } } } template<typename ItemType> void csc232::ResizableArrayStack<ItemType>::increase_size() { capacity *= 2; ItemType *temp = new ItemType[capacity]; // Only copy existing elements, not the entire array capacity for (int i = 0; i < count; i++) { temp[i] = items[i]; } delete[] items; items = temp; }
3. Bonus: Template Class Best Practice
Since this is a template class, you might run into linker errors later because the compiler needs to see the template implementation when instantiating it. To avoid this, you can either:
- Move all the
.cppimplementation code directly into theResizableArrayStack.hfile, or - Add
#include "ResizableArrayStack.cpp"at the end ofResizableArrayStack.h
Final Note
After making these changes, your main function should be able to instantiate ResizableArrayStack<int> without hitting the E0322 error. Test it out and adjust any behavior (like error handling) to match your stack interface requirements!
内容的提问来源于stack exchange,提问作者Collin Tinen

