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

Discussion in 'C++' started by girays, Jan 20, 2007.

  1. girays

    girays Guest

    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...
     
    girays, Jan 20, 2007
    #1
    1. Advertising

  2. girays

    Greg Guest

    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_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
     
    Greg, Jan 20, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
    Replies:
    2
    Views:
    393
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=
    Mar 2, 2006
  2. Replies:
    1
    Views:
    434
    red floyd
    Dec 21, 2008
  3. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,033
    James Kanze
    Dec 22, 2008
  4. James Kanze
    Replies:
    0
    Views:
    2,025
    James Kanze
    Dec 21, 2008
  5. wiegmale

    Using a template getting LNK2019 error

    wiegmale, Jul 8, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
    wiegmale
    Jul 8, 2010
Loading...

Share This Page