0

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).

AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • Yep, we need to see matrix.h and matrix.cpp as there may be a method declared but not implemented. Uuups, Mr. Seymour seems right, I didn't look at that. It looks like a template problem. – Martin Schlott Sep 02 '13 at 18:56
  • Don't write this much code without testing it. Start small and simple, add complexity a little at a time, test at every step and *never add to code that doesn't work*. – Beta Sep 02 '13 at 19:14
  • That did it, thank you very much. Also, I would never write this much code without testing it. I test every change I make at the very least to make sure it compiles and my main.cpp is always changing to make sure every method does what I want it to. – kstubblefield1 Sep 02 '13 at 19:40

0 Answers0