我的heapSort()出错了
声明与"std::vector BinaryHeap::heap排序()“不兼容(声明在
头文件中有一个类BinaryHeap,其中包含所有函数声明。堆排序如下所示:
template <typename Comparable>
class BinaryHeap
{
public:
...
vector<Comparable> heapsort();
...
private:
...
void heapify(...);
}我的heapsort()函数定义:
template <typename Comparable>
void BinaryHeap<Comparable>::heapsort()
{
// Build heap (rearrange array)
for (int i = currentSize / 2 - 1; i >= 0; i--)
heapify(array, currentSize, i);
// One by one extract an element from heap
for (int i = currentSize - 1; i > 0; i--) {
// Move current root to end
swap(array[0], array[i]);
// call max heapify on the reduced heap
heapify(array, i, 0);
}
}对我能做什么来解决这个问题有什么建议吗?我的所有其他方法都工作得很好,而且我的heapsort()显然在BinaryHeap类中。
发布于 2021-11-15 18:45:44
您的定义与声明不兼容。
声明:
vector<Comparable> heapsort();定义:
void BinaryHeap<Comparable>::heapsort() 您应该决定您的方法应该返回哪种类型,并在声明和定义中使用相同的类型。
例如,我认为void作为返回类型是有意义的(也就是说,方法不应该返回任何内容)。如果你同意,请修正你的声明:
void heapsort();但是,也许vector<Comparable>实际上是正确的返回类型?如果是这样,请修正您的定义:
template <typename Comparable>
vector<Comparable> BinaryHeap<Comparable>::heapsort()
{
...
}https://stackoverflow.com/questions/69978954
复制相似问题