0

I am trying to create an array of queues for a class project. I have written the specification file and implementation file for FIFO queue. However, when I write:

QueType<movieType> fifoQueue[6];

I get this error message:

error LNK2019: unresolved external symbol "public: __thiscall QueType::QueType(void)" (??0?$QueType@VmovieType@@@@QAE@XZ) referenced in function _main`

What do I have to add in order to be able to create an array of queues. Also, I can't use an STL queue.

Here is the main file:

#include "movieType.h" 
#include <iostream>
#include <fstream>
#include "QueType.h"

using namespace std;

int main()
{
movieType movie[9];
ifstream inFile("movie1.txt");

    //I get the error here
    QueType<movieType> fifoQueue[6];

    return 0;
}

Here is the specification file:

//definition of NodeType
template <class ItemType>
struct NodeType
{
ItemType info; // store data
NodeType* next; // sotre location of data
};

//exception class used when queue is full
class FullQueue
{};

//Exception class used when queue is empty
class EmptyQueue
{};

//templated queue class
template<class ItemType>
class QueType
{
public:
    QueType();
    //Function: class constructor
    //Precondition: none
    //Postcondition: it initializes the pointers, front and rear to null

    ~QueType();
    //Function:class destructor
    //Precondition: queue has been initialized
    //Postcondition: deallocate allocated memory

    void MakeEmpty();
    //Function: determines whether the queue is empty
    //Precondition: queue has been initialized
    //Postcondition:queue is empty

    bool IsEmpty() const;
    //Function:determines whether the queue is empty
    //Precondition:queue has been initialized
    //Postcondition:Function value = (queue is empty)

    bool IsFull() const;
    //Function:determines whether the queue is full
    //Precondition:queue has been initialized
    //Postcondition:Function value = (queue is full)

    void Enqueue(ItemType newItem);
    //Function:Adds newItem to the rear of the queue
    //Precondition:queue has been initialized
    //Postcondition:if (queue is full), FullQueue exception is thrown,
    //else newItem is at rear of queue

    void Dequeue(ItemType& item);
    //Function:removes front item from the queue and returns it in item
    //Precondition:queue has been initialized
    //Postcondition:if (queue is empty), EmptyQueue exception is thrown
    //and item is undefines, else front element has been removed from
    //queue and item is a copy of removed element

private:
    NodeType<ItemType>* front; //pointer points to the front to the queue
    NodeType<ItemType>* rear; // pointer points to the rear of the queue
};

and the specification file:

#include "QueType.h"//gives access to QueType class
#include <cstddef> //for NULL
#include <new> // for bad_alloc

template<class ItemType>
QueType<ItemType>::QueType()
{
front = NULL;
rear = NULL;
}

template <class ItemType>
QueType<ItemType>::~QueType()
{
MakeEmpty();
}

template <class ItemType>
void QueType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;//temporary pointer

while(front != NULL)
{
    tempPtr=front;
    front = front->next;
    delete tempPtr;
}
rear = NULL;
}

template <class ItemType>
bool QueType<ItemType>::IsEmpty() const
{
return (front == NULL);
}

template <class ItemType>
bool QueType<ItemType>::IsFull() const
{
NodeType<ItemType>* location;
try
{
    location = new NodeType<ItemType>
    delete location;
    return false;
}
catch(std::bad_alloc exception)
{
    return true;
}
}

template <class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem)
{
if (IsFull())
        throw FullQueue();
else
{
    NodeType<ItemType>* newNode;
    newNode = new NodeType<ItemType>;
    newNode ->info=newItem;
    newNode->next=NULL;
    if(rear== NULL)
            front= newNode;
    else
            rear->next=newNode;
    rear=newNode;
}
}
user1561949
  • 211
  • 7
  • 16

1 Answers1

1

You need to implement the functions declared in your "specification file".

This particular error message is only complaining about the missing implementation for the QueType constructor (because that's the only function of QueType you're using in main), but as soon as you start using any of the other functions, you will get similar linker errors.

In this case, because your class is templated, you need to move your definitions to the header file in order to let the compiler access them when it creates the specialisation.

For further info, see this question.

Community
  • 1
  • 1
Fraser
  • 74,704
  • 20
  • 238
  • 215