Thread: Evaluate(debug) my queue

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Evaluate(debug) my queue

    Well I have have created a queue-datastructure(circular) and tried to minimize (eliminate) all bugs. As far as I know I canīt find anyones ( yeeh sure ). If you guys (or girls) could find someone It would be very appreciated. P.S if you have any suggestions for improved stability/functionality please let me know.

    Here it is
    Code:
    class QueueFull{};
    class QueueEmpty{};
    
    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;
    };
    
    
    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] = x;
    	back++;
    	//If it reached ītopī then reset front-indicator
    	if (back == capacity)
    		back = 0;
    }
    
    template <class T>
    T CQueueArray<T>::dequeue()
    {
    	if (! currentsize)
    		throw QueueEmpty();
    	currentsize--;
    	T objecttoReturn = pArray[front];
    	front++;
    	//If it reached ītopī then reset front-indicator
    	if (front == capacity)
    		front = 0;
    	return objecttoReturn;
    }
    
    template <class T>
    int CQueueArray<T>::getsize() const
    {
    	return currentsize;
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    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???

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Originally posted by Salem
    So you need to put all your template functions in the .h file as well, so that each <type> instance can be created as necessary.
    Hmmm this is making me confusing...
    Iīve have always learned that a class interface shoud be placed in a header file (*.h) and the implementation in a source file (*.cpp) and it has always worked (non-template). Well if this "rule" doesnīt apply to templated classes there is no need for this seperation. Isnīt this a litte of bad-habit-coding(no offense Salem)??? Maybe someone else could clear this up a bit more or is templated class restricted to a single file???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM