473,396 Members | 1,914 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,396 software developers and data experts.

c++ linker error, please help!

TR
Hi

I have this very simple piece of code I can't get running (source code
below). I'm using the borland C++ compiler 6.0. The error I'm getting is:

[Linker Error] Unresolved external 'MyMap<int, int>::operator [](const
int&)' referenced from C:\PROGRAM
FILES\BORLAND\CBUILDER6\PROJECTS\MYMAP\MAIN.OBJ

The aim is to create a simple map class using templates. I try to do this
with two classes (MyMap and MyPair). Declarations in header files and
implementation in .CPP files. main.cpp uses the MyMap class.

What does the error mean? It can't find MyMap<int, int> but it's there with
a template definition isn't it? What am I overlooking?

Any help is appreciated!

--- MAIN.CPP ---

#include <vcl.h>
#include "mymap.h"
#include "mypair.h"
#include <iostream>
#pragma hdrstop

using namespace std;
int main(int argc, char* argv[]) {
MyMap<int, int> map;
map[1] = 1;
cout << map[1] << endl;
return 0;
}

--- MYMAP.H ---
#include <vector>
#include "mypair.h"
using namespace std;

template <class Key, class Element>
class MyMap {
public:
Element& operator[](const Key& key);
private:
vector<MyPair<Key, Element> > elements; };

--- MYMAP.CPP ---
#include <vector>
#include <string>
#include <iostream>
#include "mymap.h"
#include "mypair.h"
using namespace std;

template <class Key, class Element>
Element& MyMap<Key, Element>::operator[](const Key& key) {
// Find key and return if found
for(int i = 0; i < elements.size(); i++) {
if (elements[i].key == key)
return elements[i].element; }
// Key not found: create new one and return
Element element;
elements.push_back(MyPair(key, element));
return elementents[elements.size()-1].element; }

--- MYPAIR.H ---
#ifndef mypairH
#define mypairH
template<class Key, class Element>
class MyPair
{
public:
MyPair(const Key& k, const Element& e);
Key key;
Element element;
};
#endif

--- MYPAIR.CPP ---
#include "mypair.h"

template<class Key, class Element>
MyPair<Key, Element>::MyPair(const Key& k, const Element& e) {
key = k;
element = e; }
Jul 22 '05 #1
3 2601

"TR" <no******@hotmail.com> wrote in message
news:Cz****************@amsnews02.chello.com...
Hi

I have this very simple piece of code I can't get running (source code
below). I'm using the borland C++ compiler 6.0. The error I'm getting is:

[Linker Error] Unresolved external 'MyMap<int, int>::operator [](const
int&)' referenced from C:\PROGRAM
FILES\BORLAND\CBUILDER6\PROJECTS\MYMAP\MAIN.OBJ

The aim is to create a simple map class using templates. I try to do this
with two classes (MyMap and MyPair). Declarations in header files and
implementation in .CPP files. main.cpp uses the MyMap class.


Well that's where you've gone wrong. All template code should go in header
files, the rules you are used to do not apply to templates.

See the FAQ for instance

http://www.parashift.com/c++-faq-lit...html#faq-34.13

john
Jul 22 '05 #2
TR wrote:
Hi

I have this very simple piece of code I can't get running (source code
below). I'm using the borland C++ compiler 6.0. The error I'm getting
is:

[Linker Error] Unresolved external 'MyMap<int, int>::operator [](const
int&)' referenced from C:\PROGRAM
FILES\BORLAND\CBUILDER6\PROJECTS\MYMAP\MAIN.OBJ

The aim is to create a simple map class using templates. I try to do
this with two classes (MyMap and MyPair). Declarations in header files
and implementation in .CPP files. main.cpp uses the MyMap class.

What does the error mean? It can't find MyMap<int, int> but it's there
with a template definition isn't it? What am I overlooking?


#include the .cpp file at the end of your header. The compiler needs to
have the full definition to be able to instatiate a template.
Therefore, it must be defined in the translation unit you want to use
it in.

Jul 22 '05 #3
If you REALLY use borland C++ compiler 6.0 (== Borland C++ technical
Preview) - you can use template export.

//h.h
export template<class>
class A
{
////
};

//impl.cpp
#include "h.h"
//definitions
//user.cpp
#include "h.h"

int main()
{
//usage
}

But I think, you meant CBuilder 6.0?
Jul 22 '05 #4

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

Similar topics

1
by: endo55 | last post by:
Hi I've got the following errors when trying to compile a program cvision error LNK2001: unresolved external symbol _IID_IGraphBuilde cvision error LNK2001: unresolved external symbo...
3
by: Chucker | last post by:
Hi Folks, I got a Wrapper Dll around a native C++ static library. In .NET 1.1 this worked fine. When moving to .NET 2.0 I get a couple of unresolved externals / linker errors: Error 16 error...
0
by: VivekR | last post by:
I have a MFC application developed using VC++ 5. Recently I ported that to VC++ 7.1 and now I am trying to compile the MFC application with /CLR under VC++ 7.1. And I get linker errors referring to...
4
by: Mark | last post by:
Hi, I'm trying to write some classes that kind of manage themselves. A linked list, that links different types of objects. Here's the code... object.h: --- class Object { int alt;
4
by: yOtA | last post by:
I get this Linker Errors while compiling my program: Error: Unresolved external 'vminit()' referenced from C:\TESTE\TESTE.OBJ Error: Unresolved external 'vmalloc(void *, int, unsigned int,...
0
by: Ehsan68 | last post by:
Hello my problem is "Linker Error: Undefined symbol _cga_driver_far in module maze.c " then Go to Options =>Linker =>Libraries This will display a box showing Container Class Turbo...
1
by: antani | last post by:
When I add to my code this line vcg::tri::io::Importer<CMesh>::Open(mesh,"mesh.ply"); I get a linker error. Please help me. I tested a qt empty project with only main function. I added the...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
12
by: kath.neumann | last post by:
Hi, I just started to get a grip on C++ and and went through the tutorial. However, not even the standard "hello world" exercise works out :( I do get a linker error " Unresolved external...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.