using std::map in a template class, getting LNK2019 link error when creating an object from this tem

G

girays

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_type> KeyValPair;
private:
std::map<key_type, val_type> repository;
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, double> rep;

rep.insert(0, 1.1);

return 0;
}


If I don't use EntityRepository<unsigned, double> rep; 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...
 
G

Greg

girays said:
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_type> KeyValPair;
private:
std::map<key_type, val_type> repository;
public:
EntityRepository(void);
~EntityRepository(void);

val_type find(key_type const &key);
void insert(key_type const &key, val_type const &val);
};
#endif

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.

A class is implemented in a .cpp file, but a class template should be
implemented in a header file so that its definition is visible to those
source files that instantiate it. Therefore, since EntityRepository is
class template (not a class) it should be implemented in
EntityRepository.h.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top