间隔堆的实现错误

时间:2012-02-29 20:11:07

标签: c++ heap

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

这里是

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

但问题是在写代码时就像这个堆[1]。左边它显示我没有成员可用

2 个答案:

答案 0 :(得分:2)

这一个:

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

至少编译。

我在好友声明中添加了您的班级IntervalHead<T>的前瞻性声明和关键字

答案 1 :(得分:0)

您的意思是friend class IntervalHeap<T>吗?

friend本身用于声明友元函数。