When compiling my program, I am getting a link error LINK2019. The common error is that a function was declared but not defined which is not the case here. For example I have the class below that is defined in the header file dict.h
template<typename key, typename elem>
class Slist{
public:
list<KV_pair<key, elem>> *data;
Slist(int size);
~Slist();
void insert(KV_pair<key, elem> &kv);
elem &operator[](key k_val);
};
And I have defined the members as so in a separate dict.cpp file
template<typename key, typename elem>
Slist<key, elem>::Slist(int size) {
data = new list<KV_pair<key, elem>>(size);
}
template<typename key, typename elem>
Slist<key, elem>::~Slist() {
}
template<typename key, typename elem>
void Slist<key, elem>::insert(KV_pair<key, elem> &kv){
KV_pair<key, elem> *temp;
for (data->movestart();data->r_len() > 0; data->next()) {
temp = &data->get_val();
if (temp->get_key() > kv.get_key()) {
break;
}
data->insert(kv);
}
}
If I put my main()
in the dict.cpp file everything works, but when I try to put the main()
in a separate file and #include "dict.h"
I get the unresolved externals error. Although it is incorrect to do so, using #include "dict.cpp"
after #include "dict.h"
resolves the error. I have the dict.h file in the header files directory, and put both the main.cpp and dict.cpp in the source files directory/