views:

67

answers:

1

i created a template that contains a map. when i try to create an instance of that template i encounter a linking problem with the constructor and destructor. also, when i try to create an instance in main it skips the line while debugging, and doesn't even show it in the locals list. it doesn't compile "DataBase db;" unless i add "()" after db. (that's the way i try to initiate the instance in main).

the code:

h:

template <class keyVal,class searchVal, class T>  
class DataBase  
{  
private:  
    map<keyVal,pair<searchVal,T*>*> DB;  
public :  
    DataBase();  
    virtual ~DataBase();    
}; 

cpp:

#include "DataBase.h"  

template <class keyVal,class searchVal, class T>  
DataBase<keyVal,searchVal,T>::DataBase()  
{}  

template <class keyVal,class searchVal, class T>  
DataBase<keyVal,searchVal,T>::~DataBase()  
{}

thanks

+5  A: 

Add the implementation of template classes (and functions) directly in the header file:

template <class keyVal,class searchVal, class T>  
class DataBase  
{  
private:  
    map<keyVal,pair<searchVal,T*>*> DB;
public :  
    DataBase() {};  
    virtual ~DataBase() {};    
}; 
John Dibling
Explanation: http://www.parashift.com/c++-faq/templates.html#faq-35.12
Matt Kane
Nit-pick: shouldn't have a semicolon after a function definition :-). Worth noting that this requests inlining of the functions, that they can also be defined beneath the class to avoid that, and that some people actually include a cpp file from a header to allow more uniformity of style with non-templated code (though that's not particularly common and I'm not recommending it).
Tony