c++ linker error, please help!

Discussion in 'C++' started by TR, May 26, 2004.

  1. TR

    TR Guest

    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; }
     
    TR, May 26, 2004
    #1
    1. Advertising

  2. TR

    Rolf Magnus Guest

    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>::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.
     
    Rolf Magnus, May 26, 2004
    #2
    1. Advertising

  3. "TR" <> wrote in message
    news:Cz2tc.5622$...
    > 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
     
    John Harrison, May 26, 2004
    #3
  4. TR

    New_user Guest

    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?
     
    New_user, Jun 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    728
    Big Brian
    Oct 14, 2003
  2. Generic Usenet Account

    extern "C" linker error ---- please explain

    Generic Usenet Account, Jun 9, 2005, in forum: C++
    Replies:
    19
    Views:
    6,948
    Larry I Smith
    Jun 9, 2005
  3. Replies:
    1
    Views:
    354
  4. quad
    Replies:
    3
    Views:
    2,383
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 9, 2007
  5. lashnjo
    Replies:
    1
    Views:
    447
    Robert Bauck Hamar
    Jul 3, 2007
Loading...

Share This Page