I'm having some trouble with a Stack class. Everything looks fine to me, but I could be missing something. I thought it might be something with the makefile, seeing as I'm not all that great with makefiles.
I've also looked through a few different questions, but not found anything that fixed my problem.
Here is all the code I'm compiling plus the makefile.
Stack.h:
#ifndef STACK_H
#define STACK_H
#include "Node.h"
template<class Type>
class Stack
{
public:
Stack();
void push(Type );
Type pop();
bool isEmpty() const;
protected:
Node<Type> *head;
};
#endif
Stack.cpp:
#include "Stack.h"
template<class Type>
Stack<Type>::Stack()
{
head = NULL;
}
template<class Type>
void Stack<Type>::push(Type element)
{
Node<Type> *newNode;
newNode = new Node<Type>;
newNode->data = element;
newNode->next = head;
head = newNode;
}
template<class Type>
Type Stack<Type>::pop()
{
Node<Type> *current = head;
Type element = current->data;
head = head->next;
delete current;
return element;
}
template<class Type>
bool Stack<Type>::isEmpty() const
{
return head == NULL;
}
Node.h:
#ifndef NODE_H
#define NODE_H
#include "Matrix.h"
template<class Type>
struct Node
{
Type data;
Node<Type> *next;
};
#endif
main.cpp:
#include "Stack.h"
#include <iostream>
using namespace std;
int main()
{
Matrix m1;
Matrix m2(1, 2, 3, 4);
Matrix m3;
m3 = m1 + m2;
Stack<Matrix> stack;
cout << stack.isEmpty() << endl;
return 0;
}
Makefile:
all: matrix
matrix: removal main.o Matrix.o Stack.o
g++ -o matrix main.o Matrix.o Stack.o
main.o: main.cpp
g++ -c -g main.cpp
Matrix.o: Matrix.cpp
g++ -c -g Matrix.cpp
Stack.o: Stack.cpp
g++ -c -g Stack.cpp
removal:
rm -f *.o
If you need to see Matrix.h/Matrix.cpp, let me know. They're just for doing math on matrices and aren't causing any problems as far as I'm aware (they've compiled just fine).