I have a template class which name is EntityRepository and when I
compile this class I get no error. But when I use this class in a main
method I get LNK2019 linking error. std::map object is used in
EntityRepository template class. You can see the EntityRepository.h and
EntityRepository.cpp and main method below:
//EntityRepository.h
#pragma once
#ifndef ENTITYREPOSITORY_H
#define ENTITYREPOSITORY_H
#include <map>
template<class key_type, class val_type>
class EntityRepository
{
typedef std::pair<key_type, val_typeKeyValPair;
private:
std::map<key_type, val_typerepository;
public:
EntityRepository(void);
~EntityRepository(void);
val_type find(key_type const &key);
void insert(key_type const &key, val_type const &val);
};
#endif
//EntityRepository.cpp
#include ".\entityrepository.h"
using namespace std;
template<typename key_type, typename val_type>
EntityRepository<key_type, val_type>::EntityRepository(void)
{
}
template<typename key_type, typename val_type>
EntityRepository<key_type, val_type>::~EntityRepository(void)
{
}
template<typename key_type, typename val_type>
val_type EntityRepository<key_type, val_type>::find(key_type const
&key)
{
repository::const_iterator iter = repository.find(key);
if ( iter == repository.end() )
{
return NULL;
}
return iter.second();
}
template<typename key_type, typename val_type>
void EntityRepository<key_type, val_type>::insert(key_type const &key,
val_type const &val)
{
repository.insert( make_pair<key_type, val_type>(key, val) );
}
//Program.cpp
#include "EntityRepository.h"
int main(int argc, char *argv[])
{
EntityRepository<unsigned, doublerep;
rep.insert(0, 1.1);
return 0;
}
If I don't use EntityRepository<unsigned, doublerep; and
rep.insert(0, 1.1); (briefly if I don't use EntityRepository class) in
main method, it compiles and links successfully but as shown above in
method I get these 3 error messages:
1) error LNK2019: unresolved external symbol "public: void __thiscall
EntityRepository<unsigned int,double>::insert(unsigned int const
&,double const &)" (?insert@?$EntityRepository@IN@@QAEXABIABN@Z)
referenced in function _main
2)error LNK2019: unresolved external symbol "public: __thiscall
EntityRepository<unsigned int,double>::EntityRepository<unsigned
int,double>(void)" (??0?$EntityRepository@IN@@QAE@XZ) referenced in
function _main
3)error LNK2019: unresolved external symbol "public: __thiscall
EntityRepository<unsigned int,double>::~EntityRepository<unsigned
int,double>(void)" (??1?$EntityRepository@IN@@QAE@XZ) referenced in
function _main
Another important point about this problem is, If I don't seperate the
implementation in a cpp file, otherwise if I implement the class in the
header I get no error.
Does anyone help me about this problem?
Thank in advance...