Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > c++ linker error, please help!

Reply
Thread Tools

c++ linker error, please help!

 
 
TR
Guest
Posts: n/a
 
      05-26-2004
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[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; }


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      05-26-2004
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>: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.

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-26-2004

"TR" <(E-Mail Removed)> wrote in message
news:Cz2tc.5622$(E-Mail Removed)...
> 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.
>


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


 
Reply With Quote
 
New_user
Guest
Posts: n/a
 
      06-10-2004
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extern "C" linker error ---- please explain Generic Usenet Account C++ 19 06-09-2005 10:06 PM
Article : Assembly Linker (Al.exe .Net FrameWork Tools Series) Namratha Shah \(Nasha\) ASP .Net 0 11-03-2004 07:28 AM
Java 88x31 linker buttons Davenport | ATC uk RAF | 1 1 8 9 Squadron Java 1 03-03-2004 09:08 PM
Assembly Linker Question George Durzi ASP .Net 2 03-02-2004 06:22 AM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments