What is wrong with my stack?

I built a stack, and it runs fine is I put the main function in the stack.cpp file instead of using a seperate cpp file for main, but once I put main in its own file and #include my stack, it wont work


Error 1 error LNK2019: unresolved external symbol "public: __thiscall myStack<int>::myStack<int>(int)" (??0?$myStack@H@@QAE@H@Z) referenced in function _main C:\Users\Drola_000\Google Drive\C++\ADTStack\ADTStack\Source.obj ADTStack

heres my code

Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
class myStack
{
private:
	int _top;
	T *_pItems;
	int _MAX;
public:

	void push( T data );
	T pop();
	int getSize();

	bool isEmpty()		{ return _top == -1; }
	bool isFull()		{ return _top+1 == _MAX;}

	myStack<T>(int size);
	~myStack<T>(void);
};


stack cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Constructor initialize size of array, sets top to -1
template <class T>
myStack<T>::myStack(int size):_top(-1), _MAX(size)
{
	_pItems = new T[_MAX];
}

template <class T>
myStack<T>::~myStack(){ delete [] _pItems; }

//PUSH increments top, add data to top of array
template <class T>
void myStack<T>::push(T data)
{	
	_pItems[++_top] = data;
}

//POP returns top item, decrements top
template <class T>
T myStack<T>::pop()
{	
	return _pItems[_top--];
}


main cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "myStack.h"
using namespace std;

int main()
{
	myStack<int> intStack(5);

	return 0;
}


thanks!
If memory serves, you can't put the implementation of a template class in a separate file. Try removing stack.cpp and put its contents at the bottom of stack.h.
wow, that did it. Thanks a lot man!

Time to read up more on templates
An often used convention is to put your templates implementation into a separate file called "stack.tpp". Then include it at the bottom of "stack.h" which contains the templates class specification.
Topic archived. No new replies allowed.