实现模板类时C ++编译错误

时间:2014-11-06 10:19:21

标签: c++ templates

#include <functional>
#include <iostream>
#include <vector>

template <class value_type, class comparator_type>
class Heap {
    public:
    comparator_type comparator;
    explicit Heap(comparator_type some_comparator);
    std::vector<value_type> data;
    int Push(const value_type & value);
    void Delete(int index);
    const value_type & Root();
    void Pop();
    int Size();
    bool Empty();
    int Father(int index);
    int LeftSon(int index);
    int RightSon(int index);
    void SwapElements(int firstIndex, int secondIndex);
    int SiftUp(int index);
    void SiftDown(int index);
};

template <class value_type, class comparator_type>
Heap<value_type, comparator_type>::Heap(comparator_type some_comparator): comparator(some_comparator) {
}

template <class value_type, class comparator_type>    
int Heap<value_type, comparator_type>::Push(const value_type & value) {
    data.push_back(value);
    int index = data.size() - 1;
    return SiftUp(index);
}

template <class value_type, class comparator_type>    
void Heap<value_type, comparator_type>::Delete(int index) {
    const int last_index = data.size() - 1;
    if (index == last_index) {
        data.pop_back();
    } else {
        SwapElements(last_index, index);
        data.pop_back();
        if (index != 0 && comparator(data[Father(index)], data[index])) {
            SiftUp(index);
        } else {
            SiftDown(index);
        };
    };
}

template <class value_type, class comparator_type>
const value_type & Heap<value_type, comparator_type>::Root() {
    return data[0];
}

template <class value_type, class comparator_type>
void Heap<value_type, comparator_type>::Pop() {
    Delete(0);
}

template <class value_type, class comparator_type>
int Heap<value_type, comparator_type>::Size() {
    return data.size();
}

template <class value_type, class comparator_type>
bool Heap<value_type, comparator_type>::Empty() {
    return Size() == 0;
}

template <class value_type, class comparator_type>
int Heap<value_type, comparator_type>::Father(int index) {
    if (index == 0) {
        return -1;
    } else {
        return (index - 1) / 2;
    }
}

template <class value_type, class comparator_type>
int Heap<value_type, comparator_type>::LeftSon(int index) {
    int result = index * 2 + 1;
    if (result >= Size()) {
        return -1;
    } else {
        return result;
    };
}

template <class value_type, class comparator_type>
int Heap<value_type, comparator_type>::RightSon(int index) {
    int result = index * 2 + 2;
    if (result >= Size()) {
        return -1;
    } else {
        return result;
    };
}

template <class value_type, class comparator_type>
void Heap<value_type, comparator_type>::SwapElements(int firstIndex, int secondIndex) {
    std::swap(data[firstIndex], data[secondIndex]);
}

template <class value_type, class comparator_type>
int Heap<value_type, comparator_type>::SiftUp(int index) {
    if (index != 0 && comparator(data[Father(index)], data[index])) {
        SwapElements(index, Father(index));
        return SiftUp(Father(index));
    }
    return index;
}

template <class value_type, class comparator_type>
void Heap<value_type, comparator_type>::SiftDown(int index) {
    int new_place = index;
    int left_son_index = LeftSon(index);
    if (left_son_index != -1 && comparator(data[new_place], data[left_son_index])) {
        new_place = left_son_index;
    };
    int right_son_index = RightSon(index);
    if (right_son_index != -1 && comparator(data[new_place], data[right_son_index])) {
        new_place = right_son_index;
    };
    if (new_place != index) {
        SwapElements(index, new_place);
        SiftDown(new_place);
    };
}

struct IntCompare {
    bool operator() (int first, int second) const {
        return first < second;
    };
};

int main() {
    Heap<int, IntCompare> myHeap(IntCompare());
    myHeap.Push(1);
    return 0;
}

我尝试使用模板参数实现二进制堆,其中存储了值并使用了比较器。我收到表格错误

request for member `Push' in `myHeap', which is of non-class type `Heap<int, IntCompare> ()(IntCompare (*)())' 

myHeap.Push(1);

同样的事情发生在这个类的任何其他领域。我的错误是什么,如何解决?

1 个答案:

答案 0 :(得分:4)

您遇到了行中的most vexing parse问题

Heap<int, IntCompare> myHeap(IntCompare());

将其更改为

IntCompare comp;
Heap<int, IntCompare> myHeap(comp);

Heap<int,IntCompare> myHeap(IntCompare{}/*Can't be confused with a function pointer*/);

Heap<int,IntCompare> myHeap((IntCompare())/* ditto */);