Okay, guys i have been trying to define a Stack , each node also being of template type but i get dozen different types of errors when the prog tries to compile. i will paste the program which uses a char-type stack and tries to pop letter ‘e’
#ifndef STACK_LIST_H
#define STACK_LIST_H
#include "List.h"
using namespace std;
template <class T>
class Stack {
public:
T pop();
void push(T x);
T isEmpty();
T top();
private:
int size;
Node<T> * headNode;
Node<T> * currentNode;
};
#endif
Now the function definitions:
#include <iostream>
#include "Stack_list.h"
using namespace std;
template <class T>
T Stack<T>::pop(){
T x = headNode->get();
Node<T>* p = new Node<T>::Node();
p = headNode;
headNode = headNode->getNext();
delete p;
return x; }
template <class T>
void Stack<T>::push(T x){
Node<T>* newNode = new Node<T>::Node();
newNode->setNext(headNode);
newNode->set(x);
headNode=newNode;
}
template <class T>
int Stack<T>::isEmpty(){
return (headNode ==NULL);}
template <class T>
T Stack<T>::top(){
return headNode->get();
}
now the template class node:
#ifndef LIST_H
#define LIST_H
using namespace std;
/* The Node class */
template <class T>
class Node
{
public:
Node(T here){object=here;};
T get() { return object; };
void set(T object) { this->object = object; };
Node<T>* getNext() { return nextNode; };
void setNext(Node<T>* nextNode) { this->nextNode = nextNode; };
Node<T>* getPrev(){ return prevNode;};
void setPrev(Node<T>* prevNode){this->prevNode=prevNode;};
private:
T object;
Node<T>* nextNode;
Node<T>* prevNode;
};
#endif
and finally the program that evokes the classes:
#include <iostream>
#include "Stack_list.cpp"
using namespace std;
int main(){
Stack <char>s;
s.push("e");
cout<<s.pop();
}
As you can see, this is my first try at template classes. In definitions of Stack::pop() and push(T) it says, “expected type-specifier before ‘Node’”
Node<T>* newNode = new Node()is inconsistent. IsNodea class or a class template? The first time that you mention it, you treat it as a template and instantiate it withT, but the second time you treat it as a class. It can’t be both.