C++模板错误"Undefined reference to"和"Expected a type, got"

C++ template error "Undefined reference to" and "Expected a type, got"

本文关键字:Expected type got reference 错误 Undefined C++ to      更新时间:2023-10-16

我已经在这里,这里和这里浏览了之前关于这个主题的讨论,但发现我的情况是独一无二的。因此这个问题。

我正在尝试创建一个通用堆类,根据模板参数,它可以成为最小或最大堆。我正在下面粘贴我的代码。我不确定我错过了什么,但我要么收到"未定义的错误",要么收到"预期类型,得到 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>;

正如错误所说,stgt不是类型。也许你的意思是:

template class Heap<int, 0, SmallerThan<int>>;
template class Heap<int, 0, GreaterThan<int>>;

但是,我仍然建议将模板实现放在头文件中,否则您只能使用这两个显式实例化。

相关文章: