Question:
I am wondering how Visual C++ implements class templates. The code below is a simple stack class template that can take all the simple data types (char, int, float, double, etc.).
This small program will not compile. Apparently main.cpp
cannot find the constructor, push and pop. The only way this runs is if I combine STACK.H and STACK.CPP into STACK.H and then build and run. Or if I combine all into one big MAIN.CPP.
Is there a way around this? Can I have these three files separate (which I would prefer) and still get the program to compile?
// STACK.H #include#include #ifndef STACK_H #define STACH_H const int STACK_SIZE = 100; template class stack { private: int count; kind data[STACK_SIZE]; public: //Initialize the stack stack(void); void push(const kind item); kind pop(void); }; //STACK.CPP #include “stack.h” template stack ::stack(void) { count = 0; } template void stack ::push(const kind item) { data[count] = item; ++count; } template kind stack ::pop(void) { –count; return(data[count]); } // MAIN.CPP #include “stack.h” void main(void) { stack IntStack; stack FloatStack; IntStack.push(33); FloatStack.push(3.14); cout << IntStack.pop(); cout << FloatStack.pop(); }
Answer:
The latest C++ draft paper specifies that all non-exported templates that are instantiated either implicitly of explicitly need to be declared and defined before the instantiation. This means that you must supply the implementation of the template before it is used.
In the past, various compilers have given options to solve this problem byproviding special ways to implement templates in one module andexport them to the other modules. The VC++ compiler, as far as I know, doesnot have any such facility.
Given this, what I always do is include my implementation file for templatesat the bottom of the header file. For example:
//stack.h#includeNote that I call my template implementation file a .cc file so I can differentiate it from my other source files. Also, the .cc files are notused in any of the make files except as dependencies.template class stack {public: stack(size_t size); //…. othr stuff};#include “stack.cc” // end stack.h// stack.cctemplate stack ::stack(size_t size){ //. … implementation}// … other stuff// end stack.cc// main.cpp#include “stack.h”int main (){ stack i; // … }// end main.cpp
In the future, when more compilers are ANSI compliant, you will be able to export your class templates and take advantage of separate compilationfor templates.