I'm trying to write code to solve the josephus problem, for an exercise in the book I'm currently reading.
main.cc :
#include <iostream>
#include <cstdlib>
#include "node_ll.h"
int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
Node<int> *t, *x;
Node<int>::construct(N);
for (i = 2, x = Node<int>::newNode(1); i <= N; i++)
{
t = Node<int>::newNode(i);
x->insert(t);
x = t;
}
while (x != x->next())
{
for (i = 1; i < M; i++) x = x->next();
x->remove()->deleteNode();
}
std::cout << x->item() << std::endl;
return 0;
}
node_ll.h :
#ifndef NODE
#define NODE
template <class T>
class Node
{
private:
static Node* freelist;
Node* link;
T data;
public:
static Node* newNode(T);
static void construct(int);
void deleteNode();
void insert(Node*);
Node* remove();
Node* next();
T item();
};
template <class T>
void Node<T>::insert(Node* t)
{
t->link = link;
link = t;
}
template <class T>
Node<T>* Node<T>::remove()
{
Node* t{link};
link = t->link;
return t;
}
template <class T>
Node<T>* Node<T>::next()
{
return link;
}
template <class T>
T Node<T>::item()
{
return data;
}
template <class T>
Node<T>* Node<T>::newNode(T x)
{
Node* t{freelist->remove()};
t->data = x;
t->link = t;
return t;
}
template <class T>
void Node<T>::construct(int N)
{
freelist = new Node[N+1];
for(int i{0}; i < N; i++)
freelist[i].link = &freelist[i+1];
freelist[N].link = 0;
}
template <class T>
void Node<T>::deleteNode()
{
freelist->insert(this);
}
#endif
When compiling g++ throws the following error:
/usr/bin/ld: /tmp/ccNqFpW8.o: in function Node<int>::construct(int)':
main.cc:(.text._ZN4NodeIiE9constructEi[_ZN4NodeIiE9constructEi]+0x31): undefined reference to
Node::freelist'
/usr/bin/ld: main.cc:(.text._ZN4NodeIiE9constructEi[_ZN4NodeIiE9constructEi]+0x4e): undefined reference to Node<int>::freelist'
/usr/bin/ld: main.cc:(.text._ZN4NodeIiE9constructEi[_ZN4NodeIiE9constructEi]+0x65): undefined reference to
Node::freelist'
/usr/bin/ld: main.cc:(.text._ZN4NodeIiE9constructEi[_ZN4NodeIiE9constructEi]+0x85): undefined reference to Node<int>::freelist'
/usr/bin/ld: /tmp/ccNqFpW8.o: in function
Node::newNode(int)':
main.cc:(.text._ZN4NodeIiE7newNodeEi[_ZN4NodeIiE7newNodeEi]+0xe): undefined reference to Node<int>::freelist'
/usr/bin/ld: /tmp/ccNqFpW8.o:main.cc:(.text._ZN4NodeIiE10deleteNodeEv[_ZN4NodeIiE10deleteNodeEv]+0xf): more undefined references to
Node::freelist' follow
collect2: error: ld returned 1 exit status
And I have no clue what's going on