0

我正在尝试使用向量创建一个堆栈,但我似乎无法让它工作......这是我的代码:

#ifndef _STACK_VEC_TPT_H_
#define _STACK_VEC_TPT_H_
#include <stdexcept>

using namespace std;

// abstract stack class implemented using vector
template<class T>
class abs_stack_vec {
public:
    // pushes an element onto the top of the stack. 
    // grows the vector if needed.
    virtual void push(const T& elem)=0;

    // pops an element from the top of the stack.
    // does nothing if the stack is empty.
    virtual void pop()=0;

    // returns the value of the top element on the stack.
    // throws domain_error if the stack is empty.
    virtual const T& top()=0;

    // returns the number of elements currently on the stack.
    virtual unsigned size() const=0;
};

// the following class inherits from the abstract stack class
// using its own implementation of a vector
// you must implement the abstract methods push, pop, and top.
template<class T>
class mystack_vec: public abs_stack_vec<T> {
public:
    unsigned size() const {return _size;}

    // method used for growing vector when size equals capacity
    // and need to add more elements
    void grow() {
        T* temp = new T[_size * 2];
        for(unsigned i = 0; i < _size; ++i) {
            temp[i] = _values[i];
        }
        delete[] _values;
        _values = temp;
        _capacity = _size * 2;
    }

    // default constructor
    mystack_vec() {
        _capacity = 5;
        _size = 0;
        _values = new T[_capacity];
    }

    // pushes an element onto the top of the stack. 
    // grows the vector if needed.
    void push(const T& elem)
    {
        if (_size == _capacity)
        {
            grow();
        }
        _values[_size] = (elem);
        ++_size;
    }

    // pops an element from the top of the stack.
    // does nothing if the stack is empty.
    void pop()
    {
        if (_size != 0)
        {
            delete _values[_size];
            --_size;
        }
    }

    // returns the value of the top element on the stack.
    // throws domain_error if the stack is empty.
    const T& top() 
    {
        if (_size == 0)
        {
            throw domain_error("The stack is empty!");
        }
        else
        {
            return _values[_size];
        }
    }

    //destructor
    ~mystack_vec() {
        delete[] _values;
    }

    // TO-DO: YOU MUST IMPLEMENT THE FOLLOWING METHODS:
    // PUSH, POP, TOP


    // END OF TO-DO
private:
    T *_values; // array !!
    unsigned _size, _capacity;
};
#endif

尝试这种方法我得到了错误:

1>------ Build started: Project: Lab 3, Configuration: Debug Win32 ------
1>  tester.cpp
1>c:\users\jaysen\documents\data structures\lab 3\lab 3\stack_vec_tpt.h(72): error C2541: 'delete' : cannot delete objects that are not pointers
1>          c:\users\jaysen\documents\data structures\lab 3\lab 3\stack_vec_tpt.h(69) : while compiling class template member function 'void mystack_vec<T>::pop(void)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\jaysen\documents\data structures\lab 3\lab 3\tester.cpp(72) : see reference to function template instantiation 'void mystack_vec<T>::pop(void)' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\jaysen\documents\data structures\lab 3\lab 3\tester.cpp(59) : see reference to class template instantiation 'mystack_vec<T>' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\jaysen\documents\data structures\lab 3\lab 3\stack_vec_tpt.h(72): error C2059: syntax error : ';'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我也尝试过包含矢量类并使用 container.push_back() 和 push_back 等。请帮助我!

4

2 回答 2

2

直接的问题是,它_values[_size]给你动态分配数组中的最后一个T对象,而不是指针。您不能delete _values[_size];对不是指向动态分配内存的指针进行操作。

因此,您可能很想这样做delete &_values[_size];,但这也是错误的。仅仅因为您使用 , 动态分配了整个数组_values = new T[_capacity];,并不意味着您可以delete使用单个元素。您只能使用 释放整个数组delete[] _values;

现在您可以使用std::vector它来代替,但由于您没有发布您遇到的问题,所以我无法帮助您。但是,您将为标准已经提供的东西付出太多努力:

std::stack<int> s;

该类std::stack是其他容器类型的适配器。默认情况下,它使用 astd::deque作为其底层结构,这比作为堆栈更适合std::vector.

于 2013-02-25T16:31:36.767 回答
0

你不能删除int。您可以删除 int*,但在此代码中您尝试删除 int。

看到这条消息:

c:\users\jaysen\documents\data structures\lab 3\lab 3\stack_vec_tpt.h(72): error C2541: 'delete' : cannot delete objects that are not pointers
于 2013-02-25T16:27:12.450 回答