2012-03-01 52 views
-1

当我编译该代码一个更上间隔堆奇怪的情况

#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:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(43): error C2065: 'heapsize' : undeclared identifier 
1>   c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(42) : while compiling class template member function 'IntervalHeap<T>::IntervalHeap(int)' 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>   c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(214) : see reference to class template instantiation 'IntervalHeap<T>' being compiled 
1>   with 
1>   [ 
1>    T=int 
1>   ] 
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(60): error C2181: illegal else without matching if 
1>   c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_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:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(65): error C2065: 'curentsize' : undeclared identifier 
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(80): error C2065: 'minheap' : undeclared identifier 
1>c:\users\daviti\documents\visual studio 2010\projects\heap_project\heap_project\heap_project.cpp(82): error C2065: 'minheap' : undeclared identifier 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

什么是理由?

回答

0

这个错误意味着变量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一个mistmached托槽无法编译:

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; 
    ......... 

} 

的建议是:总是使用括号,即使在单句:它使代码更清晰。

+0

如何解决这个问题?请帮助我 – 2012-03-01 07:49:04

+0

让我们来看看:你声明的模板构造函数像'IntervalHeap(int heapsize = 10);'但是在模板构造函数定义中,您更改了参数名称:'IntervalHeap :: IntervalHeap(int currentsize)'。我认为你必须把'Maxsize = heapsize;'改成'Maxsize = currentize;'。接下来,你在'Insert method'中忘记了代码中的一些代码。我认为你mus在else之前添加了一个“if”(currentize <2)'评估 – 2012-03-01 08:06:54

+0

我找不到,你能解决这些错误吗?@Tio Pepe和post作为答案,我已经改变了第一个,但是找不到括号 – 2012-03-01 08:19:58

0

C:\用户\ daviti \文档\ Visual Studio 2010的\项目\ 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; 
} 
+0

但是没有最后的代码,它为什么编译? – 2012-03-01 07:35:14

+1

,因为如果你没有专门化一个模板,那么它不会在语义上检查 – LeleDumbo 2012-03-01 07:46:08

+0

例如如何修复我的插入方法?有些人说有错误 – 2012-03-01 08:06:24

3

的原因是,你名不副实你的一些变量: IntervalHeap(int heapsize=10);template<class T> IntervalHeap<T>::IntervalHeap(int currentsize)curentsize++;而不是currentsize++; minheap而不是minHeap。 C++是一种区分大小写的语言,注意这种错误。

+0

我看,当时是我写这段代码的夜晚,我的眼睛很累,非常感谢 – 2012-03-01 07:38:25