1

This should work, but it looks like something is missing. I have a queue class, which is defined in queue.h

template <typename T> 
class Queue{
 private: 
   Node<T> * head; 
   Node<T> * tail; 
   int size; 
   int capacity; 
 public:  
   Queue(int c): capacity(c) {}
   bool enqueue(T val); 
   bool dequeue(T & val); 
   bool isQueueEmpty(); 
   bool isQueueFull();  
};

and in the queue.cpp file I have

#include "queue.h" 

template <typename T> 
bool Queue<T>::enqueue(T val){
  if(isQueueFull())
    return false; 
 //set the new node
 Node<T> * temp = new Node<T>; 
 temp->val = val; 
 if(isQueueEmpty()){
   head = tail = temp; 
   head->next = nullptr; 
   head->prev = nullptr;
 }
 else {
   temp->next = head; 
   temp->prev = nullptr; 
   // link the head to it
   head->prev = temp; 
   //set the new node as head 
   head = temp; 
 }
 size++; 
 return true;  
}

dequeue and other functions are similarly defined within this function. Now I want to use them in my main as follows:

#include "queue.h"

int main(){
  Queue<int> myQ(5); 
  int val; 
  myQ.enqueue(3); 
  myQ.enqueue(4); 
}

when I compile it with c++11 it gives the following compilation error:

 undefined reference to `Queue<int>::enqueue(int)'

Any idea why?

0 Answers0