I'm trying to create a stack using a linked list. I've already got it working using integers, but now I want to implement Templates to it. There are no errors detected before compile, but after running I get these errors:
*Undefined symbols for architecture x86_84: "Stack::printStack()", referenced from: _main in main.o
"Stack::pop()", referenced from: _main in main.o
"Stack::push(int)", referenced from: _main in main.o
"Stack::~Stack()", referenced from: _main in main.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) *
I think my troubles come from having to define two templates, one in the Stack.h file and one in my LinkedList.h file. In my Stack.h file I need to call on Node, I can't call on Node without including the template arguments, and Stack.h won't recognize the template arguments without me including another template definition. Right now there are instances where I only use 'T' instead of Node, but I've tried it both ways. I feel like I've tried everything and I just can't figure out where my error is. I'm really just going for proof of concept at this point which is why I haven't added a copy constructor or additional functions for Stack. My code is below.
LinkedList.h
#ifndef Linked_List__Stack_LinkedList_h
#define Linked_List__Stack_LinkedList_h
template<class T>
struct Node {
public:
Node(Node *n, T data) : next(n), data(data) {}
Node *getNext() const { return next; }
T value() const { return data; }
private:
Node* next;
T data;
};
#endif
Stack.h
#ifndef __Linked_List__Stack__Stack__
#define __Linked_List__Stack__Stack__
#include "LinkedList.h"
#include <stdio.h>
#include <iostream>
using namespace std;
template <class T>
class Stack {
public:
Stack() : head(NULL), tail(NULL) {};
~Stack();
void push(T data);
T pop();
void printStack();
private:
Node<T> *head;
Node<T> *tail;
};
#endif /* defined(__Linked_List__Stack__Stack__) */
Stack.cpp
#include "Stack.h"
template <class T>
Stack<T>::~Stack() {
while (head) {
Node<T>* temp = head->getNext();
delete head;
head = temp;
}
head = tail = NULL;
}
template <class T>
void Stack<T>::push(T data) {
Node<T>* node = new Node<T>(head, data);
head = node;
if (head->getNext() == NULL )
tail = head;
}
template <class T>
T Stack<T>::pop() {
Node<T> *top = head;
T data;
if(head == NULL)
throw; //StackError(E_EMPTY);
data = head->value();
head = head->getNext();
delete top;
return data;
}
template <class T>
void Stack<T>::printStack() {
Node<T> *current = head;
while (current != tail) {
cout << current->value() << ", ";
current = current->getNext();
}
cout << current->value() << endl;
}
main.cpp
#include "Stack.h"
#include <iostream>
#include <stdexcept>
int main(int argc, const char * argv[]) {
Stack<int> *myIntStack = new Stack<int>();
myIntStack->push(10);
myIntStack->printStack();
myIntStack->pop();
myIntStack->printStack();
delete myIntStack;
return 0;
}