Hi I'm having some problems with c++, when I try to compile my 3 files, main.cpp, My_Stack.cpp and My_Stack.hpp I get the "Undefined symbols for architecture x86_64" error, as if I hadn't included the header file and if I add "#include "My_Stack.cpp"" instead of "#include "My_Steak.hpp"" in main.cpp it works fine.
//main.cpp
#include <iostream>
#include "My_Stack.hpp"
int main(int argc, const char * argv[]) {
My_Stack<int> s = My_Stack<int>();
s.push(1);
s.push(2);
cout << s.pop() << endl;
cout << s.pop() << endl;
}
//My_Stack.hpp
#ifndef My_Stack_hpp
#define My_Stack_hpp
#include <stdio.h>
#include <string>
using namespace std;
template<class T>
class My_Stack {
public:
My_Stack();
void push(T v);
T pop();
private:
class My_Stack_Node {
public:
T data;
My_Stack_Node* next;
My_Stack_Node(T n) {
data = n;
next = NULL;
}
};
My_Stack_Node* head;
};
#endif /* My_Stack_hpp */
//My_Stack.cpp
#include "My_Stack.hpp"
#include <string>
#include <sstream>
using namespace std;
template<class T>
My_Stack<T>::My_Stack() {
this->head = NULL;
}
template<class T>
void My_Stack<T>::push(T v) {
if (this->head == NULL) {
this->head = new My_Stack_Node(v);
}
else {
My_Stack_Node* aux = new My_Stack_Node(v);
aux->next = this->head;
this->head = aux;
}
}
template<class T>
T My_Stack<T>::pop() {
My_Stack_Node* aux = this->head;
this->head = this->head->next;
return aux->data;
}