You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++抽象类实例化报错:无法创建ResizableArrayStack对象

Fixing E0322: Cannot instantiate abstract class 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:

  • push and pop always return false (which contradicts their purpose)
  • peek is returning the last element of the array, not the actual top of the stack
  • The copy assignment operator has a pointer comparison bug
  • increase_size is 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 .cpp implementation code directly into the ResizableArrayStack.h file, or
  • Add #include "ResizableArrayStack.cpp" at the end of ResizableArrayStack.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:58:37