undeclared?
Jan 15, 2013 at 5:21pm UTC
In the insert part, it does not give an error about currentSize but why does it give this error in deletemin function?
The error is;
1>d:\game\reversi\reversi\heap.h(64): error C2065: 'currentSize––' : undeclared identifier
1> d:\game\reversi\reversi\heap.h(59) : while compiling class template member function 'Value BinaryHeap<Comparable>::deleteMin(void)'
1> with
1> [
1> Comparable=Value
1> ]
1> d:\game\reversi\reversi\main.cpp(17) : see reference to class template instantiation 'BinaryHeap<Comparable>' being compiled
1> with
1> [
1> Comparable=Value
1> ]
main part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <vector>
#include "Heap.h"
using namespace std;
int main()
{
Value a( 5, "abc" );
BinaryHeap < Value > deneme2;
deneme2.insert(a);
cout << deneme2.deleteMin().worth << endl;
system("PAUSE" );
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#ifndef HEAP_H
#define HEAP_H
#include <vector>
#include <string>
using namespace std;
template <class Comparable>
class BinaryHeap
{
public :
BinaryHeap( int capacity = 100 ):array( capacity )
{
currentSize = 0;
}
bool isEmpty( ) const ;
bool isFull( ) const ;
const Comparable & findMin( ) const ;
void insert( const Comparable & x );
Value deleteMin( );
void makeEmpty( );
// private:
int currentSize; // Number of elements in heap
vector < Comparable > array; // The heap array
void buildHeap( vector < Comparable > &Input );
void percolateDown( int hole );
};
template <class Comparable>
bool BinaryHeap<Comparable>::isEmpty() const
{
return currentSize == 0;
}
template <class Comparable>
bool BinaryHeap<Comparable>::isFull() const
{
return currentSize == capacity - 1;
}
template <class Comparable>
void BinaryHeap<Comparable>::insert( const Comparable & x )
{
int hole = ++currentSize;
for ( ; hole > 1 && x < array[ hole / 2 ]; hole /= 2 )
array[ hole ] = array[ hole / 2 ];
array[ hole ] = x;
}
template <class Comparable>
Value BinaryHeap<Comparable>::deleteMin( )
{
Value min_item;
if ( !isEmpty( ) )
{
min_item = array[ 1 ];
array[ 1 ] = array[ currentSize–– ];
percolateDown( 1 );
}
return min_item;
}
template <class Comparable>
void BinaryHeap<Comparable>::percolateDown( int hole )
{
int child;
Comparable tmp = array[ hole ];
for ( ; hole * 2 <= currentSize; hole = child )
{
child = hole * 2;
if ( child != currentSize && array[ child + 1 ] < array[ child ] )
child++;
if ( array[ child ] < tmp )
array[ hole ] = array[ child ];
else
break ;
}
array[ hole ] = tmp;
}
template <class Comparable>
void BinaryHeap<Comparable>::buildHeap( vector < Comparable > &Input)
{
array = Input; // copy input array to private array;
currentSize = Input.size();
for ( int i = currentSize / 2; i > 0; i–– )
percolateDown( i );
}
template <class Comparable>
void BinaryHeap<Comparable>::makeEmpty( )
{
currentSize = 0;
}
#endif HEAP_H
Jan 15, 2013 at 5:28pm UTC
Jan 15, 2013 at 5:37pm UTC
I got it now. This is the result of copy paste :( How did you understand this?
Jan 15, 2013 at 5:47pm UTC
The minus signs didn't have any space between them in the code block like they used to. It is also clear from the error message that it it treats the whole currentSize––
as an identifier.
Jan 15, 2013 at 5:52pm UTC
You are right, thanks.
Topic archived. No new replies allowed.