easy but couldnt see the error

Discussion in 'C++' started by jw, Nov 11, 2005.

  1. jw

    jw Guest

    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);
    }
    jw, Nov 11, 2005
    #1
    1. Advertising

  2. jw

    Guest

    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.
    , Nov 11, 2005
    #2
    1. Advertising

  3. jw

    Neil Cerutti Guest

    On 2005-11-11, jw <> wrote:
    > 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?

    --
    Neil Cerutti
    Neil Cerutti, Nov 11, 2005
    #3
  4. jw

    berkay Guest

    yes i know push_back but we must do it with out using push_back
    berkay, Nov 11, 2005
    #4
  5. jw

    Neil Cerutti Guest

    On 2005-11-11, berkay <> wrote:
    > yes i know push_back but we must do it with out using push_back


    Fool them all and use a deque and push_front, front, and
    pop_front. ;-)

    --
    Neil Cerutti
    Neil Cerutti, Nov 11, 2005
    #5
  6. jw

    Andre Kostur Guest

    Neil Cerutti <> wrote in
    news::

    > On 2005-11-11, berkay <> wrote:
    >> yes i know push_back but we must do it with out using push_back

    >
    > Fool them all and use a deque and push_front, front, and
    > pop_front. ;-)
    >


    Or how about std::stack<> ? :)
    Andre Kostur, Nov 11, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Karl Seguin
    Replies:
    1
    Views:
    435
  2. Steve Kershaw
    Replies:
    1
    Views:
    357
    Brennan Stehling
    Sep 26, 2006
  3. emre esirik(hacettepe computer science and enginee

    I couldnt find what is wrong

    emre esirik(hacettepe computer science and enginee, Nov 15, 2007, in forum: C Programming
    Replies:
    2
    Views:
    309
    user923005
    Nov 15, 2007
  4. emre esirik(hacettepe computer science and enginee

    I am sorry but I couldnt find the solving

    emre esirik(hacettepe computer science and enginee, Nov 16, 2007, in forum: C Programming
    Replies:
    2
    Views:
    274
    user923005
    Nov 16, 2007
  5. emre esirik(hacettepe computer science and enginee

    There is really important wrong, but I couldnt find

    emre esirik(hacettepe computer science and enginee, Nov 30, 2007, in forum: C Programming
    Replies:
    2
    Views:
    271
    Tor Rustad
    Nov 30, 2007
Loading...

Share This Page