I am having a little trouble implementing a queue class template, this code works fine when it isn't a template class but now gives me LNK2019 errors (indicating it can't find the function main is calling)
LNK1120: 2 unresolved externals line 0 C/C++ Problem
LNK2019: unresolved external symbol "public: __thiscall Queue<int>::~Queue<int>(void)" (??1?$Queue@H@@QAE@XZ) referenced in function _main
LNK2019: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(int)" (??0?$Queue@H@@QAE@H@Z) referenced in function _main
//queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
template <typename T>
class Queue
{
private:
struct Node { T item; struct Node * next;};
conststaticint Q_SIZE = 10;
Node * front; // pointer to front of Queue
Node * rear; // pointer to rear of Queue
int count; // current number of items in Queue
constint qsize; // maximum number of items in Queue
// preemptive definitions to prevent public copying
Queue(const Queue & q) : qsize(0) {};
Queue & operator=(const Queue & q) {return *this;};
public:
Queue(int qs = Q_SIZE); // create queue with a qs limit
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const T &); // add item to end
bool dequeue(T &); // remove item from front
};
#endif /* QUEUE_H_ */
//queue.cpp
#include "queue.h"
template <typename T>
Queue<T>::Queue(int qs)
{
qsize = qs;
count = 0;
front = rear = nullptr;
}
template <typename T>
Queue<T>::~Queue()
{
Node * temp; // create temporary address store
while(front != (void *) 0) // while the queue is not empty
{
temp = front;
front = front->next; // advance the front object to the next
delete temp; // delete the temporary data
}
}
template <typename T>
bool Queue<T>::isempty() const
{
return count == 0;
}
template <typename T>
bool Queue<T>::isfull() const
{
return count == qsize;
}
template <typename T>
int Queue<T>::queuecount() const
{
return count;
}
template <typename T>
bool Queue<T>::enqueue(const T &data) // add item to end
{
if(isfull()) // if queue is full halt queuing
returnfalse;
Node * add = new Node; // create node
add->item = data; // set node pointers
add->next = (void *) 0; // or nullptr;
count++;
if (front == (void *) 0) // if queue is empty,
front = add; // place item at front
else
rear->next = add; // else place at rear
rear = add; // have rear point to new node
returntrue;
}
template <typename T>
bool Queue<T>::dequeue(T &data) // remove item from front
{
if(front == (void *) 0) // front node is empty, queue is empty
returnfalse;
data = front->item; // set data to first item in queue
count--; // decrement item count
Node * temp = front; // save location of first item
front = front->next; // reset front to next item
delete temp; // delete former first item
if (count == 0) // if the queue is now empty set rear to point to nothing
rear = (void *)0;
returntrue;
}