Hi, i am quite new to template programming, and i am getting the Error "LNK2019 unresolved external symbol "public: int __thiscall LStack<int>::push(int)" (?push@?$LStack@H@@QAEHH@Z) referenced in function _main Data_Structures 1"
Can someone kindly shed some light on where i am messing up the linkage in the push() method.?
At using templated code you cannot split this templated code into declaration and definition parts. That's because both parts will be needed for creating the correct type, but the compiler compiles all *.cpp parts as separate units.
At your example, the compiler doesn't know at compiling your LStack.cpp which type you will use in your main.cpp file. At main.cpp you define LStack<int>, but the compiler doesn't know this information when it compiles LStack.cpp. And in consequence, the linker fails to link both parts together.
Template implementations are not really cpp files since they can't be compiled until the type information is supplied. One option is to simply add the implementations to the end of the .h file. If you must have them in a separate file, call it a .tpp (or .ipp) file and include it at the end of the .h file.