c++ - Cannot resize stack of type template -



c++ - Cannot resize stack of type template -

im trying resize stack programme keeps terminating after 'cout'. on output terminal displays 1 , programme terminates. in case t int , size set 10 default. help much appreciated.

#include <iostream> #include <fstream> using namespace std; template <typename t> class stack { public: int topstack; t* stack1; int size; void copy(const stack& other); void move(stack&& other); // constructor stack (); // destructor ~stack() { delete[] stack1; }; // re-create constructor stack (const stack&); // re-create assignment stack& operator= (const stack&); // move constructor stack (stack&&); // move assignment stack& operator= (stack&&); t& top() const; // homecoming top element void pop (); // remove top element void push(const t&); // add together element on top of stack void force (t&&); // add together element on top of stack bool empty() const; // stack empty? void clear(); // remove elements ostream& print(ostream&, stack&); void resize(); }; //default constructor template <typename t> stack<t>::stack() { size=10; stack1= new t[size]; for(int b =0; b < size; b ++) { stack1[b] = t(); } topstack =-1; } //copy constructor template <typename t> void stack<t>::copy(const stack& other) { topstack = other.topstack; stack1= new t[other.size]; size =other.size; for(int i=0; i< other.size ; i++) { stack1[i]=other.stack1[i]; } } //copy assignment template <typename t> stack<t>& stack<t>::operator =(const stack& other) { if (this == &other) homecoming *this; t* store = new t[other.size]; for(int g =0; g < other.size ; g++) { store[g]= other.stack1[g]; } delete[] stack1; this->stack1 = store; this->size = other.size; this-> topstack = other.topstack; homecoming *this; } //move constructor template<typename t> void stack<t>::move(stack && other) { topstack = other.topstack; other.topstack = 0; stack1 = other.stack1; for(int u =0; u < other.size ; u++) { other.stack1[u]=0; } size = other.size; other.size=0; } //move assignment template <typename t> stack<t>& stack<t>::operator= (stack&& other) { this->size = other.size; other.size=0; this->topstack = other.topstack; other.topstack=0; this->stack1 = other.stack1; for(int u =0; u < this->size ; u++) { other.stack1[u]=0; } homecoming *this; } //checks if stack empty template <typename t> bool stack<t>::empty() const { homecoming topstack == -1; } //resize array template<typename t> void stack<t>::resize() { cout << "dayyyy55um"; t* storage = new t[this->size*2]; cout << "dayyyyum"; for(int r=0; r < this->size ; r++) { storage[r]= this->stack1[r]; } delete[] this->stack1; this->stack1= storage; this->size = size*2; cout << "dayyyyum"; } //returns top template <typename t> t& stack<t>::top() const { if(empty()) { cout << "error: stack empty. "<< endl; return; //make throw grab statement here } homecoming stack1[topstack]; } //pop template <typename t> void stack<t>::pop() { if(empty()) { cout << "error: stack empty." << endl; return; } stack1[topstack] =0; topstack--; } //push template <typename t> void stack<t>::push(const t& q) { if(topstack < size) { topstack++; stack1[topstack] = q; }else{ resize(); } } //push template <typename t> void stack<t>::push(t&& q) { if(topstack < size) { topstack++; stack1[topstack] = q; }else{ resize(); } } //print function template <typename t> ostream& stack<t>::print(ostream& os, stack& other) { os << other.stack1 ; } template <typename t> void stack<t>::clear() { for(int g=0; g < size; g++ ) { stack1[g]=0; } } int main() { stack<int> world; world.push(9); world.push(40); world.push(40); world.push(9); world.push(40); world.push(40); world.push(9); world.push(40); world.push(40); world.push(9); world.push(40); world.push(40); cout << world.stack1[12] << endl; homecoming 0; }

that error means have out of bounds array access somewhere corrupting malloc's info structures.

the error in force function:

template <typename t> void stack<t>::push(t&& q) { if(topstack < size) { topstack++; stack1[topstack] = q; }else{ resize(); } }

when write lastly item before resize, topstack 1 less size, valid index (the lastly one, indeed), increment size, , write item in position, , that's not valid index.

by way, have lot of errors indexes al on class. advice woulbe take business relationship c++ arrays start @ 0 , end @ size-1, , revise code.

and have noticed when resize array don't add together new item?

c++ arrays stack

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -