C++模板错误"Undefined reference to"和"Expected a type, got"
C++ template error "Undefined reference to" and "Expected a type, got"
我已经在这里,这里和这里浏览了之前关于这个主题的讨论,但发现我的情况是独一无二的。因此这个问题。
我正在尝试创建一个通用堆类,根据模板参数,它可以成为最小或最大堆。我正在下面粘贴我的代码。我不确定我错过了什么,但我要么收到"未定义的错误",要么收到"预期类型,得到 st"错误。
堆.h
#ifndef __HEAP__H_
#define __HEAP__H_
#include <iostream>
#include <cstdint>
#define PARENT(x) x/2
#define LEFTCHILD(x) 2*x
#define RIGHTCHILD(x) 2*x+1
template<typename T, size_t size, typename Comparator>
class Heap
{
private:
T data[size];
Comparator comp;
uint8_t n;
//Main functions needed for maintaining heap structure
void BubbleUp(int value);
void BubbleDown(int value);
//Helper functions
void Swap(int value1, int value2);
public:
bool Insert(int value);
bool IsEmpty();
T ExtractTop();
T PeekTop();
};
#endif
堆.cpp:
根据这里的答案,我显式实例化模板,如下所示:
#include "Heap.h"
template<typename T, size_t size, typename Comparator>
bool Heap<T, size, Comparator>::Insert(int value)
{
if(n >= size)
return false;
data[n] = value;
BubbleUp(n);
n++;
return true;
}
template<typename T, size_t size, typename Comparator>
bool Heap<T, size, Comparator>::IsEmpty()
{
return (n == 0);
}
template<typename T, size_t size, typename Comparator>
T Heap<T, size, Comparator>::ExtractTop()
{
T value = data[0];
data[0] = data[n-1];
data[n-1] = NULL;
n--;
BubbleDown(0);
return value;
}
template<typename T, size_t size, typename Comparator>
T Heap<T, size, Comparator>::PeekTop()
{
return data[0];
}
template<typename T, size_t size, typename Comparator>
void Heap<T, size, Comparator>::BubbleUp(int value)
{
int j = value;
while(j >= 1)
{
if(comp(j, PARENT(j)))
break;
else
Swap(j, PARENT(j));
j = PARENT(j);
}
}
template<typename T, size_t size, typename Comparator>
void Heap<T, size, Comparator>::BubbleDown(int value)
{
int j = value;
int k = 0;
while(j < n)
{
if(comp(LEFTCHILD(j), RIGHTCHILD(j)))
k = RIGHTCHILD(j);
else
k = LEFTCHILD(j);
if(comp(j, k))
Swap(j, k);
j = k;
}
}
template<typename T, size_t size, typename Comparator>
void Heap<T, size, Comparator>::Swap(int value1, int value2)
{
int temp;
temp = value1;
value1 = value2;
value2 = temp;
}
template<typename T>
struct SmallerThan
{
public:
bool operator()(T instance1, T instance2)
{
return(instance1 < instance2);
}
};
template<typename T>
struct GreaterThan
{
public:
bool operator()(T value1, T value2)
{
return (value1 > value2);
}
};
SmallerThan<int> st;
GreaterThan<int> gt;
template class Heap<int, 0, st>;
template class Heap<int, 0, gt>;
主.cpp
template<typename T>
struct GreaterThan
{
public:
bool operator()(T value1, T value2)
{
return (value1 > value2);
}
};
const size_t size = 8;
Heap<int, size, GreaterThan<int> > maxHeap;
编辑 1:
这是我编译上述代码时遇到的错误:
Heap.cpp:118:31: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, unsigned int size, class Comparator> class Heap’
template class Heap<int, 0, st>;
^
Heap.cpp:118:31: error: expected a type, got ‘st’
Heap.cpp:119:31: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, unsigned int size, class Comparator> class Heap’
template class Heap<int, 0, gt>;
^
Heap.cpp:119:31: error: expected a type, got ‘gt’
template class Heap<int, 0, st>;
template class Heap<int, 0, gt>;
正如错误所说,st
和gt
不是类型。也许你的意思是:
template class Heap<int, 0, SmallerThan<int>>;
template class Heap<int, 0, GreaterThan<int>>;
但是,我仍然建议将模板实现放在头文件中,否则您只能使用这两个显式实例化。
相关文章:
- C++:"Expected '(' for function-style cast or type construction"错误
- 如何解决"expected constructor, destructor, or type conversion before ‘(’ token"错误?
- 为什么我有"Expected type specifier error"?
- 内存集调用位置的"error: expected constructor, destructor, or type conversion before '(' token"
- 默认模板参数结果为"expected type-specifier"
- "expected unqualified-id before 'void'" & "string in class does not name a type" 错误
- 编译错误 C++ "was not declared in this scope" "expected type specifier"等
- C++生成器错误"Type name expected" time_t
- Qt:"Expected type-specifier"出现在一台设备上,而不是另一台具有相同代码的设备上
- 在同一函数中"this declaration ahs no type specifier"和"expected a declaration"错误
- C++模板错误"Undefined reference to"和"Expected a type, got"
- 在另一个类声明中创建类的对象时"Expected a type specifier"错误
- 在自定义类中实例化自定义类时出现"expected type-specifier"错误
- 模板类编译"expected constructor, destructor, or type conversion before ‘<’ token"错误
- 错误 : "expected constructor, destructor, or type conversion before 'typedef' "
- "Expected '(' for function-style cast or type construction"错误是什么意思?
- C++编译错误"expected constructor, destructor, or type conversion before ‘class’"
- C++ 代码中的错误 "expected constructor, destructor, or type conversion before ‘(’ token" 和"no matching fun
- 尝试从模板化类制作 .o 时"expected type-specifier"
- 函数声明错误,"expected constructor, destructor, or type conversion before ';' token"