间隔堆上还有一个奇怪的情况

one more strange situation on interval heap

本文关键字:情况 有一个      更新时间:2023-10-16

当我编译这段代码时

#include <iostream>
#include<algorithm>
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
    };
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
    Maxsize=heapsize;
    //determine  number of array positions needed
    //array will be heap[0:n-1];
    int n=Maxsize/2+Maxsize%2+1;
    heap=new TwoElement<T>[n];
    currentsize=0;
    }
template<class T>
IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
{
    if (currentsize==Maxsize)
        exit(1);
    if (currentsize<2)
    {
        if(x<heap[1].left)
            heap[1].left=x;
        else heap[1].right=x;
    else
    {
        heap[1].left=x;
        heap[1].right=x;
    }
    curentsize++;
    return *this;

    }
    int lastnode=currentsize/2+currentsize%2;
    bool minHeap;
    if (currentsize%2)
         if (x<heap[lastnode].left)
             minHeap=true;
         else
         {
             lastnode++;
             if (x<=heap[lastnode/2].left)
                 minheap=true;
             else
                 minheap=false;
         }
         if (minHeap) //fix min heap interval heap
         {
             int i=lastnode;
             while (i!=1 && x<heap[i/2].left){
                 heap[i].left=heap[i/2].left;
                 i/=2;

             }
             heap[i].left=x;
             currentsize++;
             if (currentsize%2)
                 heap[lastnode].right=heap[lastnode].left;

         }
         else
         {
             int i=lastnode;
             while(i!=1 &&  x>heap[i/2].right){
                  heap[i].right=heap[i/2].right;
                  i/=2;

             }
             heap[i].right=x;
             currentsize++;
             if (currentsize%2)
                 heap[lastnode].left=heap[lastnode].right;

         }
          return *this;

}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMax(T &x)
{
    if (currentsize==0)
        exit(1);
    x=heap[1].right;
    int lastnode=currentsize/2+currentsize%2;
    T y;
    if (currentsize %2)
    {
        y=heap[lastnode].left;
        lastnode--;
    }
    else{
        y=heap[lastnode].right;
        heap[lastnode].right=heap[lastnode].left;

    }
    currentsize--;
    int i=1,ci=2;
    while(ci<lastnode){
        if (ci<lastnode && heap[ci].right<heap[ci+1]) ci++;
         if (y>=heap[ci].right) break;
         //can't put y in  heap[i]
         heap[i].right=heap[ci].right;
         if (y<heap[ci].left)
             ::swap(y,heap[ci].left);
         i=ci;
         ci*=2;
    }
    heap[i].right=y;
    return *this;

}
template<class T>
IntervalHeap<T>& IntervalHeap<T>::DeleteMin(T &x)
{
     if (currentsize==0)
         exit(1);
     x=heap[1].left;
     int lastnode=currentsize/2+currentsize%2;
     T y;
     if (currentsize%2)
     {
         y=heap[lastnode].left;
         lastnode--;
     }
     else
     {
         y=heap[lastnode].right;
         heap[lastnode].right=heap[lastnode].left;
     }
     currentsize--;
     int i=1;
     int ci=2;
     while(ci<=lastnode) //find place for y
     {

          if (ci<lastnode && heap[ci].left>heap[ci+1].left) ci++;
          if (y<=heap[ci].left) break;
          heap[i].left=heap[ci].left;
          if (y>heap[ci].right)
              ::swap(y,heap[ci].right);
          i=ci;
          ci*=2;
     }
     if (i==lastnode && currentsize%2)
         heap[lastnode].left=heap[lastnode].right;
     else 
         heap[i].left=y;
     return *this
}

int main(){

    return 0;
}

没有错误,但是当我添加以下代码时

IntervalHeap<int>Heap;
    Heap.Insert(2);
    Heap.Insert(30);
    Heap.Insert(3);
    Heap.Insert(30);
    Heap.Insert(4);
    Heap.Insert(25);
    Heap.Insert(10);
    Heap.Insert(15);
    Heap.Insert(5);
    Heap.Insert(12);
    Heap.Insert(8);
    Heap.Insert(16);
    Heap.Insert(4);
    Heap.Insert(10);
    Heap.Insert(5);
    Heap.Insert(8);
    Heap.Insert(16);
    Heap.Insert(9);
    Heap.Insert(15);

发生此错误

1>------ Build started: Project: heap_project, Configuration: Debug Win32 ------
1>  heap_project.cpp
1>c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(43): error C2065: 'heapsize' : undeclared identifier
1>          c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(42) : while compiling class template member function 'IntervalHeap<T>::IntervalHeap(int)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(214) : see reference to class template instantiation 'IntervalHeap<T>' being compiled
1>          with
1>          [
1>              T=int
1>          ]
1>c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(60): error C2181: illegal else without matching if
1>          c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(52) : while compiling class template member function 'IntervalHeap<T> &IntervalHeap<T>::Insert(const T &)'
1>          with
1>          [
1>              T=int
1>          ]
1>c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(65): error C2065: 'curentsize' : undeclared identifier
1>c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(80): error C2065: 'minheap' : undeclared identifier
1>c:usersdavitidocumentsvisual studio 2010projectsheap_projectheap_projectheap_project.cpp(82): error C2065: 'minheap' : undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

什么是理性?

原因是您错误地命名了一些变量: IntervalHeap(int heapsize=10);template<class T> IntervalHeap<T>::IntervalHeap(int currentsize)curentsize++;而不是currentsize++; minheap而不是minHeap.C++是一种区分大小写的语言,请注意此类错误。

此错误意味着未声明变量heapsize

template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
    Maxsize=heapsize;   // here is the problem
    //determine  number of array positions needed
    //array will be heap[0:n-1];
    int n=Maxsize/2+Maxsize%2+1;
    heap=new TwoElement<T>[n];
    currentsize=0;
}

什么是heapsize?是Interval<>类属性吗?

此外,IntervalHeap<T>::Insert中有一个无法编译的错误制动器:

template<class T>
IntervalHeap<T>& IntervalHeap<T>::Insert(const T& x)
{
    if (currentsize==Maxsize)
        exit(1);
    if (currentsize<2)
    {
        if(x<heap[1].left)
            heap[1].left=x;
        else heap[1].right=x;
    } // this is new bracket
    else
    {
        heap[1].left=x;
        heap[1].right=x;
    }
    curentsize++;
    return *this;
    .........
}

建议:始终使用括号,即使是单个句子:它使代码更清晰。

C:\Users\daviti\Documents\Visual Studio 2010\projects\heap_project\heap_project\heap_project.cpp(43(: 错误 C2065:"堆大小":未声明的标识符

这是来自编译器的一个非常明确的错误消息,也可以在您的代码中验证:

template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
{
    Maxsize=heapsize; // <-- where does this `heapsize` come from?
    //determine  number of array positions needed
    //array will be heap[0:n-1];
    int n=Maxsize/2+Maxsize%2+1;
    heap=new TwoElement<T>[n];
    currentsize=0;
}
相关文章: