Linker Errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MiniDisc_2k2

    Linker Errors

    I have a class:

    template <class T>
    class linkedlist
    {
    protected:
    class node
    {
    public:
    T data;
    node* next;
    node* prev;
    node() {next = NULL; prev = NULL;};
    ~node();
    } *first;
    public:
    ~linkedlist() { if (first!=NULL) delete first;};
    void AddElement(cons t T &data);
    T AddElement(istr eam &input);
    void Append(const linkedlist* const data);
    linkedlist() {first = NULL;};
    };

    template <class T>
    linkedlist<T>:: node::~node()
    {
    if (next!=NULL)
    delete next;

    if (prev!=NULL)
    prev->next = NULL;
    }


    That code is in linkedlist.h (some code was taken out). Please note that I
    have defined NULL (if undefined) to be 0.

    In main.cpp:

    #include <iostream>
    #include "linkedlist .h"

    using namespace std;
    int main(int argc, char* argv[])
    {
    ...some code...

    linkedlist<int> intlist;

    .... some more code...

    return 0;
    }


    When I compile this code, I get a linker error:

    Linking...
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall
    linkedlist<int> ::node::~node(v oid)" (??1node@?$link edlist@H@@QAE@X Z)
    Debug/main.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    Of course, I know what this means. It's saying that I never defined the
    destructor to node. I thought I did. What am I doing wrong?

    --
    MiniDisc_2k2
    To reply, replace nospam.com with cox dot net.



  • John Harrison

    #2
    Re: Linker Errors


    "MiniDisc_2 k2" <MattDelB@nospa m.com> wrote in message
    news:h6pNa.4070 $ZT5.272@news2. east.cox.net...[color=blue]
    > I have a class:
    >
    > template <class T>
    > class linkedlist
    > {
    > protected:
    > class node
    > {
    > public:
    > T data;
    > node* next;
    > node* prev;
    > node() {next = NULL; prev = NULL;};
    > ~node();
    > } *first;
    > public:
    > ~linkedlist() { if (first!=NULL) delete first;};
    > void AddElement(cons t T &data);
    > T AddElement(istr eam &input);
    > void Append(const linkedlist* const data);
    > linkedlist() {first = NULL;};
    > };
    >
    > template <class T>
    > linkedlist<T>:: node::~node()
    > {
    > if (next!=NULL)
    > delete next;
    >
    > if (prev!=NULL)
    > prev->next = NULL;
    > }
    >
    >
    > That code is in linkedlist.h (some code was taken out). Please note that I
    > have defined NULL (if undefined) to be 0.
    >
    > In main.cpp:
    >
    > #include <iostream>
    > #include "linkedlist .h"
    >
    > using namespace std;
    > int main(int argc, char* argv[])
    > {
    > ...some code...
    >
    > linkedlist<int> intlist;
    >
    > ... some more code...
    >
    > return 0;
    > }
    >
    >
    > When I compile this code, I get a linker error:
    >
    > Linking...
    > main.obj : error LNK2001: unresolved external symbol "public: __thiscall
    > linkedlist<int> ::node::~node(v oid)" (??1node@?$link edlist@H@@QAE@X Z)
    > Debug/main.exe : fatal error LNK1120: 1 unresolved externals
    > Error executing link.exe.
    >
    > Of course, I know what this means. It's saying that I never defined the
    > destructor to node. I thought I did. What am I doing wrong?
    >[/color]

    VC++ .NET gives

    'member functions of nested classes of a template class cannot be defined
    outside the class'

    which I think is good advice although not standard C++.

    g++ 3.2 compiles it fine.

    I'd move the definition back inside the class.

    john


    Comment

    • MiniDisc_2k2

      #3
      Re: Linker Errors

      > VC++ .NET gives[color=blue]
      >
      > 'member functions of nested classes of a template class cannot be defined
      > outside the class'
      >
      > which I think is good advice although not standard C++.
      >
      > g++ 3.2 compiles it fine.
      >
      > I'd move the definition back inside the class.
      >
      > john
      >
      >[/color]

      thanks, yeah that fixed it. Shouldn't have though....


      Comment

      Working...