Templates really really confuse me.
I understand what they do, but in terms of implementing them I seem to get an unending parade of syntax errors.
I'm writing a program that evaluates a mathematical expression by converting it to a postfix expression, and I need a template of my node class. I need a character stack to perform the conversion from infix to postfix, and I need an integer stack to evaluate the postfix expression.
What I'm doing is having the stack be made of a linked list. Now, the stack itself doesn't care about the datatype...only the element in the node defines the datatype.
So I'm presuming that I only a need a template for my node class. Except I can't get it compile....or even come remotely close to compiling.
I made sure to get the logic stuff out of the way, so I know my program converts from infix to postfix correctly - I just need to build a template, which
should be easy.
Here's my code...can you guys tell me what's wrong with it in terms of syntax for a template?
Thanks a lot in advance. You guys are real life savers.
And let me know if there's anything unclear about my question. It's hard for me to put myself in the shoes of someone who knows nothing about my program...let me know if there is something I didn't explain clearly enough.
Might be worth mentioning that this is all the .h file.... I just copy pasted my old .cpp file underneath the .h file, since I'm making a template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#ifndef NodeStack_h
#define NodeStack_h
#include <stdio.h>
#include <stdlib.h>
#include <string>
template <typename T>
class NodeStack
{
public:
// default constructor
NodeStack();
// constructor that takes in elements
NodeStack(T Element);
// destructor
~NodeStack();
//accessor function to retrieve the data member prev
NodeStack * getPrev();
// accessor function to set the data member prev
void setPrev (NodeStack * prev);
// accessor function to set the data member element
void setElement(T Element);
//accessor function to retrieve the data member Element
T getElement();
// display the data in the node
void display();
private:
T Element;
NodeStack * prev; // prev pointer (since this is a stack)
};
#endif
NodeStack:: NodeStack()
{
prev = NULL;
Element = ' ';
}
NodeStack:: NodeStack(T Element)
{
prev = NULL;
this -> Element = Element;
}
NodeStack:: ~NodeStack()
{
prev = NULL;
}
void NodeStack:: setPrev(NodeStack * prev)
{
this -> prev = prev;
}
NodeStack * NodeStack:: getPrev()
{
return (prev);
}
T NodeStack:: getElement()
{
return (Element);
}
void NodeStack:: display()
{
cout << "Element: " << Element << endl;
}
|