Ошибка при использовании параметра типа шаблона

0

У меня возникла проблема с компиляцией класса шаблона. Я считаю, что правильно строю скелет, но компилятор не согласен со мной. Я надеялся, что кто-то сможет взглянуть на это и указать мне в правильном направлении, где я ошибаюсь.

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

Я делаю это так же, как и везде, и это ошибки, которые я получаю:

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() {

Для справки это мой заголовочный файл, который, я думаю, в порядке:

#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
Теги:
templates
compiler-errors

1 ответ

3

class T и int MAX_SIZE определяют параметры и, как и обычные переменные (а не T - переменная), как только они определены, вам нужно только имя для их использования:

template<class T, int MAX_SIZE>
PQueue<T, MAX_SIZE>::PQueue() {
    buildHeap();
}
  • 0
    Большое спасибо, я так глуп, что должен был заметить. Я думаю, это то, что происходит, когда вы копируете и вставляете
  • 0
    @ Даг Рад, что я мог помочь. Можете ли вы принять мой ответ, нажав на галочку?

Ещё вопросы

Сообщество Overcoder
Наверх
Меню