c++ linker error, please help!

T

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>::eek:perator [](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>::eek:perator[](const Key& key) {
// Find key and return if found
for(int i = 0; i < elements.size(); i++) {
if (elements.key == key)
return elements.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; }
 
R

Rolf Magnus

TR said:
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>::eek:perator [](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.
 
J

John Harrison

TR said:
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>::eek:perator [](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-lite/containers-and-templates.html#faq-34.13

john
 
N

New_user

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top