I'm working on a program using heaps and I have a few errors that I can't quite figure out.

The first is error C2143: syntax error : missing ';' before 'using' line 2
and the second is error C2011: 'Heap' : 'class' type redefinition line 8

#include <iostream>
using namespace std;

#define MAX_SIZE 1000

//Heap Class
class Heap
{
	public:
		Heap()							// constructor
		{
			size=0;
		}
		void insertItem(int newItem)	// insert a value into Heap
		{
			item[size]= newItem;
			int child=size;
			int parent=(child - 1)/2;
			
			while(parent >= 0 && item[child] > item[parent])
			{
				//swap child and parent
				child = parent;
				parent=(child - 1)/2;
			}
			size++;
		}
		void deleteItem(int &item)		// delete the root item in Heap
		{
			if(size > 0)
			{
				int start = 0;
				item[&start]=item[&size-1];
				heapRebuild(0);
				size--;
			}
		}
		void heapRebuild(int parent)	// convert the semiheap into a heap
		{
			int child = 2 * parent + 1;
			int start=0;

			if(child < size)
			{
				int rightChild = child + 1;

				if( rightChild > size && item[start] > item[child])
				{
					child=rightChild;
				}

				if(item[parent] < item[child])
				{
					int temp = item[parent];
					item[parent] = item[child];
					item[child]=temp;
				}
			}
		}
		bool isEmpty()					// check it is empty or not
		{
			if(size>0)
				return false;
			else
				return true;
		}
	private:
		int item[MAX_SIZE];		// the array holding the items in the heap

		int size;				// the number of items in the heap
}

it also gives me: IntelliSense: expected a ';' for line 3

#include "Heap.h";

void HeapSort(int numbers[], int size)
{
	int i, temp;
	for (i = (size / 2)-1; i >= 0; i--)
    siftDown(numbers, i, size);

  for (i = size-1; i >= 1; i--)
  {
    temp = numbers[0];
    numbers[0] = numbers[i];
    numbers[i] = temp;
    siftDown(numbers, 0, i-1);
  }
}

void siftDown(int numbers[], int root, int bottom)
{
  int done=0, maxChild=0, temp;

  while (root*2 <= bottom && done!=1)
  {
    if (root*2 == bottom)
      maxChild = root * 2;
    else if (numbers[root * 2] > numbers[root * 2 + 1])
      maxChild = root * 2;
    else
      maxChild = root * 2 + 1;

    if (numbers[root] < numbers[maxChild])
    {
      temp = numbers[root];
      numbers[root] = numbers[maxChild];
      numbers[maxChild] = temp;
      root = maxChild;
    }
    else
      done = 1;
  }
}

I'm not sure what's going on, but this looks like some strict syntax issue only (i.e. your compiler is a bit capricious). Here are a few tips that can help:

- If you get some expected ';' after an #include statement, just add a ';' on an empty line following the #includes.
- Finish off your class definitions with a ';' after the closing curly brace of the class definition (i.e. class Foo { }; ).
- Don't forget to leave an empty line at the end of all your source or header files (this is actually required by the standard, but few compilers enforce the rule).

Also, make sure you are compiling in C++, and not some extended language like C++/CLI.

Thanks for the suggestions the only error I'm still getting is the C2011: 'Heap' : 'class' type redefinition line 8 error

Its usually a good idea to put a include guard on you header files to prevent conflicts if they are included multiple times. Alternatively you can use #pragma once, although its not part of the standard.

#ifndef HEAP_H
#define HEAP_H
 
class Heap 
{
public:
};
 
#endif

Edited 5 Years Ago by template<>: n/a

This article has been dead for over six months. Start a new discussion instead.