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