gpt4 book ai didi

c++ - 区间堆实现错误

转载 作者:行者123 更新时间:2023-11-28 03:40:15 25 4
gpt4 key购买 nike

我正在尝试实现区间堆,但是在描述代码的开头,我有一些错误

在这里

  #include <iostream>
using namespace std;
template <class T> class IntervalHeap;
template <class T>
class TwoElement
{
friend class IntervalHeap <T>;
public:
T left,
right;
};
template<class T>
class IntervalHeap
{
public:
IntervalHeap(int heapsize=10);
~IntervalHeap(){delete[] heap;}
int size()const { return currentsize;}
T min(){
if (currentsize==0)
throw OutOfBounds();
return heap[1].left;
}
T max() { if(currentsize==0)
throw OutOfBounds();
return heap[1].right;
}
IntervalHeap<T>& Insert(const T& x);
IntervalHeap<T>& DeleteMin(T& x);
IntervalHeap<T>& DeleteMax(T& x);



private:
int currentsize;//number of elemnts in heap
int Maxsize;//max elements permited
TwoElement<T>*heap;//element array


};
int main(){



return 0;
}

编译代码后,编译正常

1>------ Build started: Project: interval_heap, Configuration: Debug Win32 ------
1> interval_heap.cpp
1> interval_heap.vcxproj -> c:\users\daviti\documents\visual studio 2010\Projects\interval_heap\Debug\interval_heap.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

但问题是,在编写这样的代码时,heap[1].left 显示 No members avialable

最佳答案

这个:

#include <iostream>
using namespace std;

template <class T> class IntervalHeap; // forward declaration

template <class T>
class TwoElement
{
friend class IntervalHeap<T>; // the "class" was missing
public:
T left,right;
};

int main(){
return 0;
}

至少编译。

我添加了你类(class)的前向声明IntervalHead<T>和关键字 class int friend declaration.

关于c++ - 区间堆实现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505876/

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