473,625 Members | 3,318 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 EntityRepositor y 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
EntityRepositor y template class. You can see the EntityRepositor y.h and
EntityRepositor y.cpp and main method below:

//EntityRepositor y.h
#pragma once
#ifndef ENTITYREPOSITOR Y_H
#define ENTITYREPOSITOR Y_H

#include <map>

template<class key_type, class val_type>
class EntityRepositor y
{
typedef std::pair<key_t ype, val_typeKeyValP air;
private:
std::map<key_ty pe, val_typereposit ory;
public:
EntityRepositor y(void);
~EntityReposito ry(void);

val_type find(key_type const &key);
void insert(key_type const &key, val_type const &val);
};
#endif
//EntityRepositor y.cpp
#include ".\entityreposi tory.h"

using namespace std;

template<typena me key_type, typename val_type>
EntityRepositor y<key_type, val_type>::Enti tyRepository(vo id)
{
}

template<typena me key_type, typename val_type>
EntityRepositor y<key_type, val_type>::~Ent ityRepository(v oid)
{
}

template<typena me key_type, typename val_type>
val_type EntityRepositor y<key_type, val_type>::find (key_type const
&key)
{
repository::con st_iterator iter = repository.find (key);

if ( iter == repository.end( ) )
{
return NULL;
}

return iter.second();
}

template<typena me key_type, typename val_type>
void EntityRepositor y<key_type, val_type>::inse rt(key_type const &key,
val_type const &val)
{
repository.inse rt( make_pair<key_t ype, val_type>(key, val) );
}
//Program.cpp
#include "EntityReposito ry.h"

int main(int argc, char *argv[])
{
EntityRepositor y<unsigned, doublerep;

rep.insert(0, 1.1);

return 0;
}
If I don't use EntityRepositor y<unsigned, doublerep; and
rep.insert(0, 1.1); (briefly if I don't use EntityRepositor y 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
EntityRepositor y<unsigned int,double>::in sert(unsigned int const
&,double const &)" (?insert@?$Enti tyRepository@IN @@QAEXABIABN@Z)
referenced in function _main

2)error LNK2019: unresolved external symbol "public: __thiscall
EntityRepositor y<unsigned int,double>::En tityRepository< unsigned
int,double>(voi d)" (??0?$EntityRep ository@IN@@QAE @XZ) referenced in
function _main

3)error LNK2019: unresolved external symbol "public: __thiscall
EntityRepositor y<unsigned int,double>::~E ntityRepository <unsigned
int,double>(voi d)" (??1?$EntityRep ository@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 3364
girays wrote:
I have a template class which name is EntityRepositor y 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
EntityRepositor y template class. You can see the EntityRepositor y.h and
EntityRepositor y.cpp and main method below:

//EntityRepositor y.h
#pragma once
#ifndef ENTITYREPOSITOR Y_H
#define ENTITYREPOSITOR Y_H

#include <map>

template<class key_type, class val_type>
class EntityRepositor y
{
typedef std::pair<key_t ype, val_typeKeyValP air;
private:
std::map<key_ty pe, val_typereposit ory;
public:
EntityRepositor y(void);
~EntityReposito ry(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 EntityRepositor y is
class template (not a class) it should be implemented in
EntityRepositor y.h.

Greg

Jan 20 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8441
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 std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Test<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Test@H@@@Z ) referenced in function _main
2
2278
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 #include <map> #include <string>
1
1962
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> { // private: protected: std::map<std::string, T*> tMapOf; // map container
2
3392
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, MathFuncPtr> predefinedFunctions; // lots of other stuff void makeDictionary(){ predefinedFunctions=&F::f_sin(); } };
1
3546
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 program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
1
6471
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 under Visual Studio .NET 2003 in a Win32 Console Project // VectorInsert.cpp : Defines the entry point for the console application / #include "stdafx.h #include "VectorInsert.h #ifdef _DEBU
13
9659
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
1759
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 MAP::value_type::second_type & val) { ...
7
6253
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 basic theory is we wrap the functions we want exposed to the console in a function with a prototype of int func(State*)
0
8256
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8497
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.