469,953 Members | 2,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

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

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...

Jan 20 '07 #1
1 3058
girays wrote:
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

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

Jan 20 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Weddick | last post: by
1 post views Thread by Bob | last post: by
2 posts views Thread by Serengeti | last post: by
1 post views Thread by Saeed Amrollahi | last post: by
13 posts views Thread by kamaraj80 | last post: by
1 post views Thread by Paul Dubuc | last post: by
7 posts views Thread by DevNull | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.