easy but couldnt see the error

J

jw

it crashes but why?thanks for the helps.
#include<vector>
#include<iostream>
using namespace std;

template<class Object>
class Stack{
private:
vector<Object>myVec;
int topOfStack;
public:
Stack()
{
topOfStack=-1;
}
void push(Object x)
{
if(topOfStack==myVec.size()-1) {myVec.resize(myVec.size()*3);}//if
stack is full resize it
myVec[++topOfStack]=x;

}
};

void main(){
Stack<int>m;
m.push(5);
m.push(10);
}
 
G

gpriv

You keep resizing vector to 0. And you don't need to resize it, there
is push_back method. STL has much better ways to do what you are
trying to do and vector is not the best choice.
 
N

Neil Cerutti

it crashes but why?thanks for the helps.
#include<vector>
#include<iostream>
using namespace std;

template<class Object>
class Stack{
private:
vector<Object>myVec;
int topOfStack;
public:
Stack()
{
topOfStack=-1;
}
void push(Object x)
{
if(topOfStack==myVec.size()-1) {myVec.resize(myVec.size()*3);}//if

What is zero times three?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top