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>: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>: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; }
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>: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>: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; }