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

时间:2015-01-11 20:25:38

标签: c++ templates heap

我已经完成了之前关于此主题hereherehere的讨论,但发现我的情况是独一无二的。因此这个问题。

我正在尝试创建一个通用堆类,它取决于模板参数,可以成为最小或最大堆。我在下面粘贴我的代码。我不确定我错过了什么,但我得到了一个"未定义的错误"或者"期待一种类型,得到st"错误。

Heap.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

Heap.cpp:

根据答案here,我明确地实例化了如下所示的模板:

#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>;

Main.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’

1 个答案:

答案 0 :(得分:1)

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>>;

但是,我仍然建议将您的模板实现放在头文件中,否则您只会遇到这两个显式实例。