gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:02:41 27 4
gpt4 key购买 nike

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

这个类的任何其他字段都会发生同样的事情。我的错误是什么?我该如何解决?

最佳答案

您遇到了 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 */);

关于C++实现模板类时编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26776913/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com