ベクターを使用してスタックを作成しようとしていますが、うまくいかないようです...これが私のコードです:
#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 などを使用してみました。助けてください!