Hey im tryin to create a Bineary heap. Any advice on my code. Im getting errors with my constructor and idk what to do.
Error 1 error C2228: left of '.size' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\set example\set example\set.cpp 72
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <iterator>
using namespace std;
template<class B>
class heap
{
public:
heap();
heap ( vector<B> &v);
B size() const;
bool isEmpty();
void deletemin();
void push( const B & x);
void makeempty();
void pop();
void percolateup(const B & x);
B heapsize;
void buildHeap();
B lessThan;
B theSize;
vector<B> array;
void percolatedown( B hole);
};
template<class B>
heap<B>::heap() : array(15), theSize(0)
{
}
/*template<class B>
heap<B>::heap(const vector<B> & v): array(v.size()+1),theSize(v.size()+1)
{
for(int i = 0; i < v.size(); i++)
{
array[ i + 1] = v[ i ];
buildHeap();
}
}
*/
template<class B>
void heap<B>::deletemin()
{
if(isEmpty())
throw UnderflowException ();
array [ 1] = array [ theSize --];
percolatedown(1);
}
template<class B>
void heap<B>::percolateup( const B & x)
{
array [ 0 ] = x;
if ( theSize + 1 == array.size())
array.resize(array.size() * 2 +1);
int hole = ++theSize;
for(; x< array [hole /2 ]; hole /=2)
{
array[ hole ] = array [ hole / 2 ];
array [ hole ] = x;
}
}
template<class B>
B heap<B>:: size() const
{
return heapsize.size() -1;
}
template<class B>
void heap<B>::push( const B & )
{
heapsize.push_back(x);
heapsize [ 0 ] = x;
int hole = size();
for( ; lessThan( heapsize [ hole/2],x); hole / 2)
{
heapsize[hole] = heapsize [ hole / 2];
heapsize[hole] = x;
}
}
template<class B>
void heap<B>::pop()
{
if( empty())
throw UnderflowException();
B hole = 1;
B child;
B tmp = heapsize.back(); heapsize.pop_back();
int theSize = size();
for(; hole *2 <= theSize; hole = child)
{
child = hole * 2;
if( child != theSize && lessThan( heapsize[child], heapsize[child+1]))
child++;
if ( lessThan(tmp, heapsize [child]. heapsize[child+1]))
heapsize[hole] = heapsize[child];
else
break;
}
if(!empty())
{
heapsize[hole] = tmp;
}
}
void main()
{
heap<int> h;
h.percolateup(1);
h.percolateup(5);
h.percolateup(10);
h.percolateup(17);
h.percolateup(15);
h.percolateup(100);
cout<< h.size()<<endl;
}
Error 1 error C2228: left of '.size' must have class/struct/union c:\users\user\documents\visual studio 2010\projects\set example\set example\set.cpp 72
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <iterator>
using namespace std;
template<class B>
class heap
{
public:
heap();
heap ( vector<B> &v);
B size() const;
bool isEmpty();
void deletemin();
void push( const B & x);
void makeempty();
void pop();
void percolateup(const B & x);
B heapsize;
void buildHeap();
B lessThan;
B theSize;
vector<B> array;
void percolatedown( B hole);
};
template<class B>
heap<B>::heap() : array(15), theSize(0)
{
}
/*template<class B>
heap<B>::heap(const vector<B> & v): array(v.size()+1),theSize(v.size()+1)
{
for(int i = 0; i < v.size(); i++)
{
array[ i + 1] = v[ i ];
buildHeap();
}
}
*/
template<class B>
void heap<B>::deletemin()
{
if(isEmpty())
throw UnderflowException ();
array [ 1] = array [ theSize --];
percolatedown(1);
}
template<class B>
void heap<B>::percolateup( const B & x)
{
array [ 0 ] = x;
if ( theSize + 1 == array.size())
array.resize(array.size() * 2 +1);
int hole = ++theSize;
for(; x< array [hole /2 ]; hole /=2)
{
array[ hole ] = array [ hole / 2 ];
array [ hole ] = x;
}
}
template<class B>
B heap<B>:: size() const
{
return heapsize.size() -1;
}
template<class B>
void heap<B>::push( const B & )
{
heapsize.push_back(x);
heapsize [ 0 ] = x;
int hole = size();
for( ; lessThan( heapsize [ hole/2],x); hole / 2)
{
heapsize[hole] = heapsize [ hole / 2];
heapsize[hole] = x;
}
}
template<class B>
void heap<B>::pop()
{
if( empty())
throw UnderflowException();
B hole = 1;
B child;
B tmp = heapsize.back(); heapsize.pop_back();
int theSize = size();
for(; hole *2 <= theSize; hole = child)
{
child = hole * 2;
if( child != theSize && lessThan( heapsize[child], heapsize[child+1]))
child++;
if ( lessThan(tmp, heapsize [child]. heapsize[child+1]))
heapsize[hole] = heapsize[child];
else
break;
}
if(!empty())
{
heapsize[hole] = tmp;
}
}
void main()
{
heap<int> h;
h.percolateup(1);
h.percolateup(5);
h.percolateup(10);
h.percolateup(17);
h.percolateup(15);
h.percolateup(100);
cout<< h.size()<<endl;
}