"LNK2019 - Unresolved external Symbol" while using templates

Discussion in 'C++' started by Mastadex, Sep 30, 2005.

  1. Mastadex

    Mastadex Guest

    So here is my Code:

    template <class T>
    struct Nodes
    {
    T *Data; // The Image
    char Name[50]; // The Name of the image

    Nodes<T> *Prev;
    Nodes<T> *Next;
    };
    typedef struct Nodes Node;


    template <class T>
    class List
    {
    Node *Current;

    public:

    List();
    ~List();

    };

    ---------------------------------------------------

    #include "List.h"

    template <class T>
    List<T>::List()
    {

    }

    template <class T>
    List<T>::~List()
    {

    }

    ====================================================

    error LNK2019: unresolved external symbol "public: __thiscall
    List<class Image>::~List<class Image>(void)"
    (??1?$List@VImage@@@@QAE@XZ) referenced in function main...

    Thats the error I get. Is this one of those subtle c++ bugs that I need
    to work around? my main is quite simple, all it does is instanciate the
    class. thats all:

    List<int> foo;

    Im completely confused.
    Mastadex, Sep 30, 2005
    #1
    1. Advertising

  2. Mastadex

    mlimber Guest

    Mastadex wrote:
    > So here is my Code:
    >
    > template <class T>
    > struct Nodes
    > {
    > T *Data; // The Image
    > char Name[50]; // The Name of the image
    >
    > Nodes<T> *Prev;
    > Nodes<T> *Next;


    There's no need for the <T> here. Nodes is fine.

    > };
    > typedef struct Nodes Node;


    This is unnecessary in C++. You can drop this line and not change any
    other code.

    >
    >
    > template <class T>
    > class List
    > {
    > Node *Current;
    >
    > public:
    >
    > List();
    > ~List();
    >
    > };
    >
    > ---------------------------------------------------
    >
    > #include "List.h"
    >
    > template <class T>
    > List<T>::List()
    > {
    >
    > }
    >
    > template <class T>
    > List<T>::~List()
    > {
    >
    > }
    >
    > ====================================================
    >
    > error LNK2019: unresolved external symbol "public: __thiscall
    > List<class Image>::~List<class Image>(void)"
    > (??1?$List@VImage@@@@QAE@XZ) referenced in function main...
    >
    > Thats the error I get. Is this one of those subtle c++ bugs that I need
    > to work around? my main is quite simple, all it does is instanciate the
    > class. thats all:
    >
    > List<int> foo;
    >
    > Im completely confused.


    You need to put the template function definitions in the header file.
    See this FAQ:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.7

    Also, unless you're doing homework here, I'd suggest using std::list
    instead of reinventing the wheel.

    Cheers! --M
    mlimber, Sep 30, 2005
    #2
    1. Advertising

  3. Mastadex

    John Fullman Guest

    mlimber's got it exactly.
    You need to put all templates in the header file.
    There should be no .cpp file for templated classes.
    John Fullman, Sep 30, 2005
    #3
  4. Mastadex

    Default User Guest

    John Fullman wrote:

    > mlimber's got it exactly.


    Got what exactly?



    Brian

    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
    Default User, Sep 30, 2005
    #4
  5. Mastadex

    Mastadex Guest

    thanks guys.

    im still a bit rusty with C++ after my vacation :/
    Mastadex, Oct 1, 2005
    #5
    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. JKop
    Replies:
    3
    Views:
    468
  2. NewsGroup
    Replies:
    3
    Views:
    665
    Michael
    Oct 27, 2006
  3. Trent
    Replies:
    9
    Views:
    618
    Trent
    Jun 29, 2007
  4. Replies:
    1
    Views:
    317
  5. Tauqir
    Replies:
    2
    Views:
    2,246
    Tauqir
    Apr 9, 2010
Loading...

Share This Page