what's wrong with this code?

M

Moses

// 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.
 
T

Tim Love

Moses said:
// 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).
 
G

Gianni Mariani

Moses said:
// 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.
 
M

Moses

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).
 
M

Moses

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" ?
 
M

Moses

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:
 
M

Moses

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;"
 
T

terminator

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.
 
G

Gianni Mariani

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.
 
G

Gianni Mariani

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 ?
 

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,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top