By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,413 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,413 IT Pros & Developers. It's quick & easy.

c++ linker error, please help!

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.