ryanm
ryanm

Reputation: 724

Why won't this copy constructor in C++ compile?

I am new to copy constructors, so maybe I just don't know how they work, but I don't see why this is not working. Here is the implementations of the constructors:

Heap::Heap(void){ // New empty Heap with default capacity.
   h_capacity = 10;
   A = new int[h_capacity];
   h_size = 0;
}

Heap::Heap(int c){ // New empty Heap with capacity c.
   A = new int[c];
   h_capacity = c;
   h_size = 0;
}

Heap::Heap(int * B, int s, int c){ // New Heap with capacity c containing first s elements of B.
   A = new int[c];
   h_capacity = c;

   A = new int[h_capacity];

   for (int i = 0; i < s; i++){
      (A[i]) = B[i];
   }

   h_size = s;
}

Heap::Heap( const Heap & H ){ // Copy constructor.
   h_capicity = H->h_capicity;
   h_size = H->h_size;

   A = new int[h_capicity];

   for (int i = 0; i < h_size; i++){
      (A[i]) = H->A[i];
   }

Here is the header file:

#include <iostream>
using namespace std;

class Heap{

public:
      // Constructors and Destructor
      Heap();  // New empty Heap with default capacity.
      Heap(int c); // New empty Heap with capacity c.
      Heap(int * B, int s, int c); // New Heap with capacity c containing first s elements from B.
      Heap( const Heap & H ); // Copy constructor.
      ~Heap(); // Destructor.

      // Size and Capacity
      bool empty() {return h_size == 0;}; // True iff Heap is empty.
      int size(){ return h_size ;}; // Current size of Heap.
      int capacity(){ return h_capacity ;}; // Current capacity.

      // Operators
      Heap operator+( const Heap & H ) const; // New Heap with combined contents and capacity of operands.

      // Modifiers
      void insert(int x); // Insert element x.
      int extract_min(); // Remove and return the minimum element.

      // Display
      void display(); // Print a string representation of the heap contents to standard out.

private:
      int* A ; // Array containing heap contents.
      int  h_capacity ; // Max number of elements (= size of A).
      int h_size ; // Current number of elements.

      void trickle_up(int i);// Repairs ordering invariant after changing root.
      void trickle_down(int i);// Repairs ordering invariant after adding a leaf.
      void make_heap();// Restores ordering invariant to entier contents.
};

But when I compile I get:

heap.cpp: In copy constructor ‘Heap::Heap(const Heap&)’:
heap.cpp:34: error: ‘h_capicity’ was not declared in this scope

Upvotes: 0

Views: 1185

Answers (3)

NPE
NPE

Reputation: 500367

You've misspelt h_capacity as h_capicity.

Also, as others have pointed out, you'll need to fix the usage of ->. The compiler will complain about that in due course. :)

P.S. Don't forget to define the assignment operator.

P.P.S. It probably makes sense to mark Heap(int c); as explicit, to prevent unwanted implicit conversions from int to Heap.

Upvotes: 9

Caribou
Caribou

Reputation: 2081

h_capicity = H->h_capicity;

Mispelling of capacity

int  h_capacity

and further you will then get an error as you are accessing H (the Heap Object you are copying as a pointer) it is a reference so it should be :

h_capacity = H.h_capacity;

Look for more problems of using -> instead of .

Upvotes: 1

Tio Pepe
Tio Pepe

Reputation: 3089

You declared

int  h_capacity ; // Max number of elements (= size of A).

and you are using

h_capicity = H->h_capicity;

Upvotes: 0

Related Questions