what's wrong with this code?

Discussion in 'C++' started by Moses, May 22, 2007.

  1. Moses

    Moses Guest

    // code is here:

    #include <iostream>

    template <class T> class Stack
    {
    T* v;

    public:
    int max_size;
    int top;

    void push (T);
    T pop ();
    };

    template <class T> void Stack<T>::push (T c)
    {
    if (top == max_size) cout<<"Overflow";
    v[top]=c;
    top++;
    }

    template <class T> T Stack<T>::pop ()
    {
    if (top==0) cout<<"Underflow";
    top--;
    return v[top];
    }

    void main (void)
    {
    Stack <char> sc; // stack of characters
    sc.top=0;
    sc.max_size=100;
    sc.push('c');
    if (sc.pop ()!='c')
    {
    cout<<"Bad_pop";
    }
    else
    {
    cout<<"OK!";
    }
    }

    I can't run it. What's wrong? Any ideas?
    Thanks.
    Moses, May 22, 2007
    #1
    1. Advertising

  2. Moses

    Tim Love Guest

    "Moses" <> writes:

    >// code is here:

    I couldn't compile it because
    using namespace std;
    was missing. Also your main should probably return int.
    But your real problem is that you're not making space to store the
    stack elements anywhere. Change
    T* v;
    to
    T v[100];
    and things should work (until the stack gets bigger than 100).
    Tim Love, May 22, 2007
    #2
    1. Advertising

  3. Moses wrote:
    > // code is here:
    >
    > #include <iostream>
    >
    > template <class T> class Stack
    > {
    > T* v;
    >
    > public:
    > int max_size;
    > int top;
    >
    > void push (T);
    > T pop ();
    > };
    >
    > template <class T> void Stack<T>::push (T c)
    > {
    > if (top == max_size) cout<<"Overflow";
    > v[top]=c;
    > top++;
    > }
    >
    > template <class T> T Stack<T>::pop ()
    > {
    > if (top==0) cout<<"Underflow";
    > top--;
    > return v[top];
    > }
    >
    > void main (void)
    > {
    > Stack <char> sc; // stack of characters


    // these next 2 lines need to be in the constructor
    > sc.top=0;
    > sc.max_size=100;


    // where do you allocate the memory for "v" ?

    > sc.push('c');
    > if (sc.pop ()!='c')
    > {
    > cout<<"Bad_pop";
    > }
    > else
    > {
    > cout<<"OK!";
    > }
    > }
    >
    > I can't run it. What's wrong? Any ideas?
    > Thanks.


    Allocate some memory for the stack.
    Gianni Mariani, May 22, 2007
    #3
  4. Moses

    Moses Guest

    Thanks! I am using vc6, so I just did not write "using namespace..." etc.


    TL> the stack elements anywhere. Change
    TL> T* v;
    TL> to
    TL> T v[100];
    TL> and things should work (until the stack gets bigger than 100).
    Moses, May 22, 2007
    #4
  5. Moses

    Moses Guest

    Thank you for you reply.

    After I added constructor and destructor, I got some link errors.

    here is what I add:

    Stack (int s); //constructor
    ~Stack (); // destructor

    and errors are:

    template_test.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<char>::~Stack<char>(void)" (??1?$Stack@D@@QAE@XZ)
    template_test.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<char>::Stack<char>(void)" (??0?$Stack@D@@QAE@XZ)



    GM> // where do you allocate the memory for "v" ?
    Moses, May 22, 2007
    #5
  6. Moses

    Moses Guest

    Oh, I made a mistake. It's not link error now, but I still got compile error at line "Stack <char> sc;"

    error C2512: 'Stack<char>' : no appropriate default constructor available.



    M> Thank you for you reply.
    M> After I added constructor and destructor, I got some link errors.
    M> here is what I add:
    Moses, May 22, 2007
    #6
  7. Moses

    Moses Guest

    find more mistake...solved
    Thanks you all. :)


    M> Oh, I made a mistake. It's not link error now, but I still got
    M> compile error at line "Stack <char> sc;"
    Moses, May 22, 2007
    #7
  8. Moses

    terminator Guest

    On May 22, 6:25 pm, "Moses" <> wrote:
    > find more mistake...solved
    > Thanks you all. :)
    >
    > M> Oh, I made a mistake. It's not link error now, but I still got
    > M> compile error at line "Stack <char> sc;"


    when you declare a constructor the compilers stops auto-declaring a
    default(void-parametered) constructor therefore you cant define 'sc'
    without an inititializer now.
    So either define a default constructor too ,or type "Stack <char>
    sc(MY_INT);" instead of the above line.
    terminator, May 22, 2007
    #8
  9. Moses

    Jon Harrop Guest

    Jon Harrop, May 22, 2007
    #9
  10. Moses

    red floyd Guest

    Jon Harrop wrote:
    > Moses wrote:
    >> find more mistake...solved
    >> Thanks you all. :)

    >
    > You could have avoided all of these problems by using a language like F#
    > instead of C++.
    >


    *PLONK* Take your evangelism elsewhere.
    red floyd, May 22, 2007
    #10
  11. On May 23, 5:57 am, Jon Harrop <> wrote:
    > Moses wrote:
    > > find more mistake...solved
    > > Thanks you all. :)

    >
    > You could have avoided all of these problems by using a language like F#
    > instead of C++.


    Really, referencing a null pointer works in other languages. I'm
    impressed.
    Gianni Mariani, May 23, 2007
    #11
  12. On May 23, 12:25 am, "Moses" <> wrote:
    > find more mistake...solved
    > Thanks you all. :)
    >
    > M> Oh, I made a mistake. It's not link error now, but I still got
    > M> compile error at line "Stack <char> sc;"


    I take it that you have fixed everything now ? Why not post your
    fixed code so that someone else who searches the archives can see how
    you fixed it when that have the same problem ?
    Gianni Mariani, May 23, 2007
    #12
  13. Moses

    Jon Harrop Guest

    Jon Harrop, May 23, 2007
    #13
  14. Jon Harrop wrote:
    > Gianni Mariani wrote:
    >> Really, referencing a null pointer works in other languages. I'm
    >> impressed.

    >
    > There are no nulls and no pointers in these languages.


    Sure.
    Gianni Mariani, May 23, 2007
    #14
    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. walala
    Replies:
    3
    Views:
    2,186
    Ralf Hildebrandt
    Sep 10, 2003
  2. willem oosthuizen

    What is wrong with the following code?

    willem oosthuizen, Oct 10, 2003, in forum: VHDL
    Replies:
    9
    Views:
    1,265
  3. Matthew
    Replies:
    7
    Views:
    665
    Priscilla Walmsley
    Jan 7, 2005
  4. David. E. Goble
    Replies:
    9
    Views:
    473
    David. E. Goble
    Feb 2, 2005
  5. kiran
    Replies:
    12
    Views:
    1,116
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page