I am implementing a dictionary, in C++, for my school assignment. But
when I am testing my Item class (this is what the dictionary is
supposed to store), I get a strange error message from my compiler
(read complainer) g++:
/tmp/ccC63pgw.o: In function `main':
/tmp/ccC63pgw.o(.text+0x43): undefined reference to `Item<int,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
::Item[in-charge](int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
Can you tell me where this subtle, or not so subtle, error is located?
Thanks in advance!
Matias
item.hh:
template<typename KeyType, typename ElemType>
class Item {
public:
Item(KeyType, ElemType);
Item(const Item &);
~Item() { delete _key; delete _elem; }
KeyType *getKey() const { return _key; }
ElemType *getElement() const { return _elem; }
private:
KeyType *_key;
ElemType *_elem;
};
item.cc:
#include "item.hh"
template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(KeyType k, ElemType e) {
_key = new KeyType(k);
_elem = new ElemType(e);
}
template<typename KeyType, typename ElemType>
Item<KeyType, ElemType>::Item(const Item<KeyType, ElemType> &item) {
_key = new KeyType(*(item.getKey()));
_elem = new ElemType(*(item.getElement()));
}
test.cc:
#include "item.hh"
#include <iostream>
#include <string>
int main() {
Item<int, std::string> anItem(5, "a string");
std::cout << "key " << *(anItem.getKey())
<< "element " << *(anItem.getElement()) << "." <<
std::endl;
return 0;
}