使用模板类型参数时出错
Error using template type parameter
我在编译模板类时遇到了一些问题。我相信我正确地构造了骨架,但编译器不同意我的观点。我希望有人能看看这个,并指出我哪里出错了。
// 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
class T
和int MAX_SIZE
定义参数,就像常规变量(不是T
变量)一样,一旦定义了它们,您只需要名称即可使用它们:
template<class T, int MAX_SIZE>
PQueue<T, MAX_SIZE>::PQueue() {
buildHeap();
}
相关文章:
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在不同的模板参数包之间分发非类型参数包
- 如何在使用容器和字符串时强制使用显式分配器类型参数
- c++:为什么我在给出模板函数的所有类型参数时都会出错,而在省略参数时还好
- 使用bool非类型参数实例化类模板时出错
- 使用模板类型参数时出错
- 显式转换模板非类型参数时出错