Hmm have diffuculties to divide the code in a *.cpp and *.h file.
In main.cpp I have
Code:
#include <iostream>
class QueueFull{};
class QueueEmpty{};
#include "queuear.h"
using namespace std;
int main()
{
CQueueArray<int> myQueue(5);
return 0;
}
In queuear.h
Code:
#ifndef _QUEUEAR_H_
#define _QUEUEAR_H_
//This is a circular queue
template <class T>
class CQueueArray
{
public:
CQueueArray(int capa = 10);
~CQueueArray();
void enqueue(const T &x);
T dequeue();
int getsize() const;
private:
int front;
int back;
int currentsize;
int capacity;
T *pArray;
};
#endif
and finally in queuear.cpp
Code:
#include "queuear.h"
template <class T>
CQueueArray<T>::CQueueArray(int capa):
front(0), back(0), currentsize(0)
{
capacity = capa;
pArray = new T[capacity];
}
template <class T>
CQueueArray<T>::~CQueueArray()
{
delete []pArray;
}
template <class T>
void CQueueArray<T>::enqueue(const T &x)
{
if (currentsize == capacity)
throw QueueFull();;
currentsize++;
//(*pArray)[back++];
back++;
if (back == capacity)
back = 0;
pArray[back];
}
template <class T>
T CQueueArray<T>::dequeue()
{
if (! currentsize)
throw QueueEmpty();
currentsize--;
front++;
//If it reached ītopī then reset front-indicator
if (front == capacity)
front = 0;
return pArray[front];
}
template <class T>
int CQueueArray<T>::getsize() const
{
return currentsize;
}
and this compiles fine. The problem is when it tries to link the program
ain.obj : error LNK2001: unresolved external symbol "public: __thiscall CQueueArray<int>::~CQueueArray<int>(void)" (??1?$CQueueArray@H@@QAE@XZ)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall CQueueArray<int>::CQueueArray<int>(int)" (??0?$CQueueArray@H@@QAE@H@Z)
Debug/Queue Array.exe : fatal error LNK1120: 2 unresolved externals
From where a can se it has to do something with the constructor and destructor but I canīt find the error!!!
When I copy and paste the whole code in a single file it links with no problems. What is happening???