template <class DataType>
class Stack
{
private:
DataType* stacklist;
//......
public:
Stack(int NewSize);
void Push(const DataType& newelement);
/....
};
Here is the constructor source::
template <class DataType>
Stack<DataType>::Stack(int NewSize)
{
stacklist = new DataType[NewSize];
top = -1;
}
Here is the Push Method source::
template <class DataType>
void Stack<DataType>:;Push(const DataType& newelement)
{
top++;
stacklist[top] = newelement;
}
And here is a snip of the driver main program::
int main(void)
{
Stack<char*> OrderingStack(3);
char temp[256];
strcpy(temp, "hi");
OrderingStack.Push(temp);
}
I have been trying to get this working for the past few days and am really running out of ideas,...If I use integers instead of character arrays it works fine, I know there probably has to be some special handling for the char arrays...but I don't know what? If anyone could give me a push in the right direction, I'd appreciate it.
Thanks
-Tim
Edited by - Peddler on 10/26/00 6:46:28 PM
Edited by - Peddler on 10/26/00 6:48:26 PM
Character Arrays in Template Stack Classes?
Having a bit of trouble getting character arrays to work correctly in a Template Stack Class. What happens is that it can Push on one character array fine and pop, peek it without problems...but once I push a second one on, both character arrays in the stack take on the new value, so once the stack goes more then 1 item deep, all the values in the stack will take on whatever was pushed on last?
Here is the important parts of the Stack Definition::
October 26, 2000 07:08 PM
What do you do to add the new value...
Do you set ''temp'' to a new value or do you copy into the
old array?
If you just copy into the old array then that''s your problem...
Your reusing the pointer... and putting that pointer on the stack....
Try to make a copy of the data in that pointer from either the app side of the template side... that should help. Just keep the pointer unique... =)
Let me know how it works out...
Lawrence Ohab
Do you set ''temp'' to a new value or do you copy into the
old array?
If you just copy into the old array then that''s your problem...
Your reusing the pointer... and putting that pointer on the stack....
Try to make a copy of the data in that pointer from either the app side of the template side... that should help. Just keep the pointer unique... =)
Let me know how it works out...
Lawrence Ohab
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement