Amolak Chandel
Amolak Chandel

Reputation: 55

Unable to make object

I wrote a program for binary heap given below

#include<iostream>
  using namespace std;

class BinaryHeap
        {

           private:
            int   currentSize;  // Number of elements in heap
            int array[];        // The heap array

            void buildHeap( );
            void percolateDown( int hole );
           public:
            bool isEmpty( ) const;
            bool isFull( ) const;
            int findmini( ) const;

            void insert( int x );
            void deleteMin( );
            void deleteMin( int minItem );
            void makeEmpty( );

   public :
      BinaryHeap( int capacity )
        {
          array[capacity + 1];
         currentSize = 0;
        }
};


 int main()
    {
         int resp, ch, choice;
         int n, i;
         Binaryheap b;
         cout << "enter the size of heap" << endl;
         cin >> n;
         BinaryHeap(n);

    return (0);
    }

while compiling it gives error as - 'binaryheap' was not declared in this scope at the line where i wrote the code BinaryHeap b;
What is the cause of the eroor and how it could be resolved?

Upvotes: 0

Views: 88

Answers (5)

Gaetan Poirier
Gaetan Poirier

Reputation: 1

Your current error is simply a case sensitivity typo, "Binaryheap b;" isn't declared because.. it actually isn't. Your class is named "BinaryHeap"(capital h!), so the object in question needs to be named with a capital h as well.

This gives you another error. When you defined your constructor for BinaryHeap, you immediately lost the use of your default constructor. This explains why "b" remains underlined; it's declaration is incomplete!

You can fix this in two ways:

  1. Make yourself a parameter-less constructor for BinaryHeap.
  2. Complete "b"'s declaration.

Hope this helps! Best of luck!

Upvotes: 0

Suvarna Pattayil
Suvarna Pattayil

Reputation: 5239

You have defined only a parameterised constructor. When you do this, the default constructor is not made available by default. This is to avoid cases which can lead to the object not being initialised properly.

Consider,

class Dog
{
int height;

public:
Dog(int x)
{
    height = x;
}

};

In this case, you should have a height for every Dog. If a default constructor is provided by default you can do Dog d i.e A Dog with no height (Not Good!)

Either define a default constructor for BinaryHeap or call it only when you pass an int like in BinaryHeap(n);

You seem to call Binaryheap b. h instead of H

Upvotes: 1

Daniel Daranas
Daniel Daranas

Reputation: 22624

C++ is case sensitive. Change Binaryheap b; to BinaryHeap b;.

Besides, your constructor takes one parameter, so you need to construct your objects using a constructor call with one parameter. See Huytard's answer for an example.

Upvotes: 4

Braden
Braden

Reputation: 1

You need to make sure that you use the same case declaration where ever you call it.

Upvotes: 0

Son-Huy Pham
Son-Huy Pham

Reputation: 1899

You defined the constructor with an int so that's what you probably intend to use.

  BinaryHeap b(20);

Upvotes: 1

Related Questions