Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Mar 27, 1997

Microsoft VC++ implementation of class templates

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 by providing special ways to implement templates in one module and export them to the other modules. The VC++ compiler, as far as I know, does not have any such facility.

Given this, what I always do is include my implementation file for templates at the bottom of the header file. For example:

//stack.h
#include 

template 
class stack 
{
public:
	stack(size_t size);

	//.... othr stuff

};

#include "stack.cc" 
// end stack.h

// stack.cc
template 
stack::stack(size_t size)
{
	//. ... implementation
}

// ... other stuff
// end stack.cc

// main.cpp
#include "stack.h"

int main ()
{
	stack i;
	// ... 
}
// end main.cpp
Note 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 not used in any of the make files except as dependencies.

In the future, when more compilers are ANSI compliant, you will be able to export your class templates and take advantage of separate compilation for templates.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date