473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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 3348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Yoon-Soo Lee | last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the following template code. The error messages is error LNK2019: unresolved external symbol "class...
2
by: Weddick | last post by:
I decided to try creating a map with microsoft visual C++ 6. When building this small app I get 95 warnings which make no sense to me. Anybody else see this before? Thanks, // CODE SAMPLE...
1
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap template<class T> class cMap : public cList<T> { ...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
1
by: Paul Dubuc | last post by:
Question: In the following template function signature template <class MAP> int matchAbbreviationMap( const MAP & choices, typename MAP::value_type::first_type & arg, typename...
7
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command console for a MUD server I have written. The...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.