I'm having a problem with creating my own stack class and implementing it in a postfix calculator program I made. I was trying to make the stack.h file, but it keeps telling me stack is not a template

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;

template <typename T>
class ListNode
{
public:
  ListNode();	
private:
    T value;
    ListNode<T> *next;
friend class  stack<T>;
    	
};

template <class T>
class stack
{
public:
  stack( ); 
  ~stack( ); 
  void push(T const&);
void makeEmpty();
  bool isEmpty() const;
  void pop();
 T top();
  
 private:
  ListNode<T> *topOfStack;

};
#endif

and here is my implementation:

#include <iostream>
#include "stack.h"
using namespace std;


template<typename T>
ListNode<T>::ListNode(){
  next = NULL;
 value = NULL;
};


template<typename T>
stack<T>::stack(){
  topOfStack = NULL;
};
template<typename T>
stack<T>::~stack(){
  makeEmpty();
}
template<typename T>
T stack<T>::top(){
  return topOfStack->value;
}
template<typename T>
void stack<T>::pop(){
  ListNode<T> *newnode = new ListNode<T>();
  topOfStack = topOfStack->next;
  delete newnode;
}
template<typename T>
bool stack<T>::isEmpty()const{
  return topOfStack==NULL;
}
template<typename T>
void stack<T>::makeEmpty(){
  while(!isEmpty()){
    pop();
  }
}

template<typename T>
void stack<T>::push(T const& x){
  ListNode<T> *newnode = new ListNode<T>();
  newnode->value = x;
  newnode->next = topOfStack;
  topOfStack = newnode;
}

int main(){
  return 0;
}

This is my first time working with templates so im not sure if its right

template class and its definition has to be on the same file, unless
your compiler supports the keyword export.


and you need a forward declaration :

#ifndef STACK_H
#define STACK_H
#include <iostream>
using namespace std;
template<typename T>
class stack;

template <typename T>
class ListNode
{
public:
  ListNode();	
private:
    T value;
    ListNode<T> *next;
friend class  stack<T>;
    	
};

template <class T>
class stack
{
public:
  stack( ); 
  ~stack( ); 
  void push(T const&);
void makeEmpty();
  bool isEmpty() const;
  void pop();
 T top();
  
 private:
  ListNode<T> *topOfStack;

};
#endif

Edited 7 Years Ago by firstPerson: n/a

The stack.cpp is compiling fine now thank you, but when I try to implement it in my post fix calculator program it doesn't work. It goes back to the header file, but then claims that stack is not a template and the use of stack is ambgious and first declared as 'template<class T> struct stack' here. Should I add my postfix implementation

Edited 7 Years Ago by killerqb: n/a

Here's the header of the postfix

#ifndef POSTFIXCALCULATOR_H
#define POSTFIXCALCULATOR_H
#include <iostream>
#include <stack>
using namespace std;





class postfixCalculator {
	public:
  int  evaluate(string word);			
	
};
#endif

This code worked when using #include <stack>, but I'm trying to implement my own

#include <iostream>
#include "postfixCalculator.h"
#include "stack.h"
using namespace std;

stack<int> stacky;
string calc;
string a;
int x;
int y;
int i;

int postfixCalculator::evaluate(string word){
  calc = word;
  while(calc.length()!=0){
   i = calc.find(" ");
 if( i >0 ){
  a = calc.substr(0,i);
 calc = calc.substr(i+1); 

 if(a.length() > 1)
   stacky.push(atoi(a.c_str()));
 else{
 switch(a[0]){
      case '+':      
       x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x + y << endl;
        stacky.push(x + y);
        break;
      case '-': 

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y - x << endl;
        stacky.push(y - x);
        break;
      case '*':

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x*y << endl;
        stacky.push(x*y);
        break;
case '~':

        x = stacky.top();
        stacky.pop();
	// cout << stacky.top() << endl;
        stacky.push(x*-1);
        break;
      case '/':                                                                          

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y/x << endl;
        stacky.push(y/x);
        break;
      default:
        stacky.push(atoi(a.c_str()));        
        break;
 }
 }
  }
else{
 switch(calc[0]){
      case '+':      
       x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << x + y << endl;
        stacky.push(x + y);
        break;
      case '-': 

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y - x << endl;
        stacky.push(y - x);
        break;
      case '*':

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
        //cout << x*y << endl;
        stacky.push(x*y);
        break;
case '~':

        x = stacky.top();
        stacky.pop();
	// cout << stacky.top() << endl;
        stacky.push(x*-1);
        break;
      case '/':                                                                          

        x = stacky.top();
        stacky.pop();
        y = stacky.top();
        stacky.pop();
	// cout << y/x << endl;
        stacky.push(y/x);
        break;
      default:
        stacky.push(atoi(calc.c_str()));        
        break;
 }
 break;
  
  }
  }
   return stacky.top();
 
}

thanks, but now its throwing me undefined reference errors when I try to run it

Nvm, got it wasn't compiling it with the stack.cpp, whoops, thank you so much

This question has already been answered. Start a new discussion instead.