尝试使用向量实现堆栈时出错

时间:2013-02-25 16:24:24

标签: c++ stack

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

#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 ==========

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

2 个答案:

答案 0 :(得分: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类是其他容器类型的适配器。默认情况下,它使用std::deque作为其底层结构,它比std::vector更适合作为堆栈。

答案 1 :(得分: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