当我编译这段代码时
#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 :(得分:3)
原因是你错误地命名了一些变量:
IntervalHeap(int heapsize=10);
和
template<class T>
IntervalHeap<T>::IntervalHeap(int currentsize)
,
curentsize++;
代替currentsize++;
minheap
代替minHeap
。 C ++是一种区分大小写的语言,请注意这种错误。
答案 1 :(得分:0)
c:\ users \ daviti \ documents \ visual studio 2010 \ projects \ heap_project \ heap_project \ heap_project.cpp(43):error C2065:'heapsize':undeclared identifier
这是编译器非常明确的错误消息,也可以在您的代码中验证:
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;
}
答案 2 :(得分: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
中有一个无法编译的braket:
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;
.........
}
建议:总是使用括号,即使是单句也是如此:它使代码更清晰。