Linker Errors

M

MiniDisc_2k2

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(const T &data);
T AddElement(istream &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(void)" (??1node@?$linkedlist@H@@QAE@XZ)
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?
 
J

John Harrison

MiniDisc_2k2 said:
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(const T &data);
T AddElement(istream &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(void)" (??1node@?$linkedlist@H@@QAE@XZ)
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?

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
 
M

MiniDisc_2k2

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top