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

Recommended Answers

All 7 Replies

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

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

Post the whole code.

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();
 
}

call your stack something like Stack. Because the standard c++ has
a stack class.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.