间隔堆上还有一个奇怪的情况
one more strange situation on interval heap
当我编译这段代码时
#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;
}
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我的输出中有一个额外的 0,为什么会这样
- 节点是否为空,即使它有一个值?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 在 Stream C++ 文本之前有一个额外的换行符
- 我的调试器说我有一个隔离错误错误,但无法告诉我在哪里,在非常特殊的情况下发生
- 是否有一个术语来表示"single mutex deadlock"(具有非递归互斥锁的死锁类型情况)?
- 是否存在代码在c++11中有一个序列点而在c++03中没有的情况
- 是否有一个标准容器允许在不使迭代器无效的情况下插入元素
- Winsock: Overlapped AcceptEx表示在没有客户端连接的情况下有一个新的连接
- 在c++中是否有一个很好的理由在不改变属性的情况下引用它?