templates

I tryed to make a stack class to test working with templates. It has no compile errors but in the test program(compiled with GNU under the Eclipse IDE) it gives errors "undefined reference to: 'stack<int>::stack()'" and all the other functions.

stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef STACK_H_
#define STACK_H_

#include <cstdlib>
using namespace std;

template<typename _type>
class stack {
private:
	_type *data;
	int size;
public:
	int getsize(void);
	void push(_type _data);
	_type pop(void);
	stack(void);
	stack(int _size, _type _data);
	virtual ~stack();
};

#endif 


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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "stack.h"

template<typename _type>
int stack<_type>::getsize(void) {
	return size;
}

template<typename _type>
void stack<_type>::push(_type _data) {
	data = (_type*)realloc(data, (size+1)*sizeof(_type));
	if(data != NULL) {
		data[size++] = _data;
	}
}

template<typename _type>
_type stack<_type>::pop(void) {
	return data[size-1];
}

template<typename _type>
stack<_type>::stack(int _size, _type _data) {
	data = (_type*)malloc(_size*sizeof(_type));
	if(data != NULL) {
		size = _size;
		for(int  i = 0; i < size; i++)
			data[i] = _data;
	}
}

template<typename _type>
stack<_type>::stack(void) {
	size = 0;
}

template<typename _type>
stack<_type>::~stack() {
	delete[] data; delete &size;
}


and the test program
1
2
3
4
5
6
7
8
9
#include "stack.h"
#include <cstdio>
using namespace std;

int main() {
	stack<int> mystack;
	mystack.push(5);
	printf("%d", mystack.pop());
}


Can someone please tell me what i did wrong?
I don't know heaps about stacks yet but this may help you - http://www.cprogramming.com/tutorial/computersciencetheory/stackcode.html
I'm not sure what could be causing the error, but I just noticed a very big error.
Your constructor stack() is not initializing data. There's no telling what will happen if you try to push() something afterwards. At least make data equal 0 so that you know that you must allocate memory when push()ing.

Avoid such C constructs as malloc() and realloc().

Most vector-like structures reserve more memory than they actually need to reduce the number of reallocations. What would happen if your stack was 100,000,000 elements long and I decided to push another one?
this class was not intended for use. it was just a practice so i could learn templates. i realize that it has gaps in it's logic, but if it could stack 5 elements and retrieve them correctly i had proof of concept, and that's all i needed. i'm just curios as to why it doesn't compile correctly.
What happens if you include stack.cpp in main, instead?
Problem solved. I think there was something wrong with my copy of the IDE and compiler(i compiled it at school). But if i may ask another question. What would you recommend instead of malloc and realloc? i chose these because with realloc i could enlarge the data vector while copying the data all in one line.
The C++ Way is always using new and delete. You can use memcpy() if you want to copy the data.
The original problem was that normally template functions/classes are implemented in the header they are declared in. Fail that, you have to tell your compiler to generate code for stack instantiated with int somewhere. AFAIK this second option is compiler dependent.

@LolFactor - I'd use a std::deque as the underlying storage, since it will manage the memory for you.

Otherwise std::copy() would be preferable to memcpy() since memcpy() implements a bit-wise copy and std::copy() implements member-wise.

Topic archived. No new replies allowed.