Template/class link error
-
First time useing templates and allthough they arnt needed im useing them to get used to templates. Im getting erros with i try to declare my key_table (the class) like key_table< apstring, int> kt; it give the error: error LNK2001: unresolved external symbol "public: __thiscall key_table< class apstring,int>::key_table< class apstring,int>(void)" (??0?$key_table@Vapstring@@H@@QAE@XZ) i dont have anything else in my main except for the statment: key_table< apstring, int> kt; i dont know why it would be complaing about it but if im using the templates wrong please correct me.
#include "assoc.h"
template< class S, class L> class key_table
{
public:
key_table();
key_table(const key_table< S,L> &table);int length(); bool empty(); void store(const S &target); bool get\_string(const S &target); //bool remove(const S &target); key\_table operator =(const key\_table< S,L> &table); protected: int table\_length; int current\_string; assoc< S,L> data\[MAX\_TABLE\_SIZE\];
};
#include "onetable.h"
#include template< class S, class L>
key_table< S,L>::key_table(){
table_length=0;
current_string=0;
}template< class S, class L>
key_table< S,L>::key_table(const key_table< S,L> &table){
table_length=table.table_length;for(int j=0;j< MAX\_TABLE\_SIZE;j++) data\[j\]=table.data\[j\];
}
template< class S, class L>
int key_table< S,L>::length(){
return table_length;
}template< class S, class L>
bool key_table< S,L>::empty(){
if(table_length==0)
return true;
else
return false;
}template< class S, class L>
void key_table< S,L>::store(const S &target){
int probe = 0;
bool found = false;L item=target.length(); assert(table\_length< MAX\_TABLE\_SIZE); assoc a(target, item); data\[table\_length+1\]=a; ++table\_length;
}
template< class S, class L>
bool key_table< S,L>::get_string(const S &item){
bool success=false;if(empty) success=false; else if(current\_string=table\_length) success=false; else{ current\_string++; item=data\[current\_string\].get\_string(); } return success;
}
-
First time useing templates and allthough they arnt needed im useing them to get used to templates. Im getting erros with i try to declare my key_table (the class) like key_table< apstring, int> kt; it give the error: error LNK2001: unresolved external symbol "public: __thiscall key_table< class apstring,int>::key_table< class apstring,int>(void)" (??0?$key_table@Vapstring@@H@@QAE@XZ) i dont have anything else in my main except for the statment: key_table< apstring, int> kt; i dont know why it would be complaing about it but if im using the templates wrong please correct me.
#include "assoc.h"
template< class S, class L> class key_table
{
public:
key_table();
key_table(const key_table< S,L> &table);int length(); bool empty(); void store(const S &target); bool get\_string(const S &target); //bool remove(const S &target); key\_table operator =(const key\_table< S,L> &table); protected: int table\_length; int current\_string; assoc< S,L> data\[MAX\_TABLE\_SIZE\];
};
#include "onetable.h"
#include template< class S, class L>
key_table< S,L>::key_table(){
table_length=0;
current_string=0;
}template< class S, class L>
key_table< S,L>::key_table(const key_table< S,L> &table){
table_length=table.table_length;for(int j=0;j< MAX\_TABLE\_SIZE;j++) data\[j\]=table.data\[j\];
}
template< class S, class L>
int key_table< S,L>::length(){
return table_length;
}template< class S, class L>
bool key_table< S,L>::empty(){
if(table_length==0)
return true;
else
return false;
}template< class S, class L>
void key_table< S,L>::store(const S &target){
int probe = 0;
bool found = false;L item=target.length(); assert(table\_length< MAX\_TABLE\_SIZE); assoc a(target, item); data\[table\_length+1\]=a; ++table\_length;
}
template< class S, class L>
bool key_table< S,L>::get_string(const S &item){
bool success=false;if(empty) success=false; else if(current\_string=table\_length) success=false; else{ current\_string++; item=data\[current\_string\].get\_string(); } return success;
}
You can't declare the implementation of template classes in your .cpp files. Try moving all the functions inline into your template classes .h file and see how you go. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com
-
You can't declare the implementation of template classes in your .cpp files. Try moving all the functions inline into your template classes .h file and see how you go. Hope this helps. ------------------------ Derek Waters derek@lj-oz.com
but how do i keep from having one big .h file, i do not wish to declare all my classes in the .h file, thats what the .cpp is for? is it proper/normal programing to defile all the functions in the .h?
-
but how do i keep from having one big .h file, i do not wish to declare all my classes in the .h file, thats what the .cpp is for? is it proper/normal programing to defile all the functions in the .h?
You've hit a limitation of VC and templates, the template code has to be visible in every CPP file, so it has to be in header files. --Mike-- "Jobs that don't allow you to visit the Lounge 25 times a day at the minimum are not worth having anyway." -- Nish, 3/28/2002 My really out-of-date homepage Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan and Jamie Salé.