Doug
Doug

Reputation: 11

Error using template type parameter

I am having some trouble compiling a template class. I believe I am constructing the skeleton correctly, but the compiler disagrees with me. I was hoping someone could take a look at this and point me in the right direction as to where I am going wrong.

// default constructor, construct an empty heap
template<class T, int MAX_SIZE>
PQueue<class T, int MAX_SIZE>::PQueue() {
    buildHeap();
}

I am doing this the same way everywhere else and these are the errors I'm getting:

PQueue.cpp:14:14: error: using template type parameter ‘T’ after ‘class’
 PQueue<class T, int MAX_SIZE>::PQueue() {
              ^
PQueue.cpp:14:29: error: template argument 1 is invalid
 PQueue<class T, int MAX_SIZE>::PQueue() {
                             ^
PQueue.cpp:14:29: error: template argument 2 is invalid
PQueue.cpp:14:39: error: declaration of template ‘template<class T, int MAX_SIZE> int PQueue()’
 PQueue<class T, int MAX_SIZE>::PQueue() {

Just for reference this is my header file which I think is fine:

#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H

#include <iostream>
using namespace std;

// Minimal Priority Queue implemented with a binary heap
// Stores item of type T
template< class T, int MAX_SIZE >
class PQueue{
public:
    PQueue(); // default constructor, construct an empty heap
    PQueue(T* items, int size); // construct a heap from an array of elements
    void insert(T); // insert an item; duplicates are allowed.
    T findMin(); // return the smallest item from the queue
    void deleteMin(); // remove the smallest item from the queue
    bool isEmpty(); // test if the priority queue is logically empty
    int size(); // return queue size
private:
    int _size; // number of queue elements
    T _array[MAX_SIZE]; // the heap array, items are stoed starting at index 1
    void buildHeap(); // linear heap construction
    void moveDown(int); // move down element at given index
    void moveUp(); // move up the last element in the heap array
};

#include "PQueue.cpp"

#endif

Upvotes: 0

Views: 1697

Answers (1)

David G
David G

Reputation: 96800

class T and int MAX_SIZE define the parameters, and, just like regular variables (not that T is a variable), once they're defined all you need is the name to use them:

template<class T, int MAX_SIZE>
PQueue<T, MAX_SIZE>::PQueue() {
    buildHeap();
}

Upvotes: 3

Related Questions