Podo
Podo

Reputation: 709

Incompatible Class Declaration c++

I have class NumberArray in NumberArray.h

class NumberArray
{
private:
  double *aPtr;
  int arraySize;

public:
  NumberArray(int size, double value);

  // ~NumberArray() { if (arraySize > 0) delete [ ] aPtr;}
  //commented out to avoid problems with the
  //default copy constructor
  void print() const; 
  void setValue(double value);
};

When I go to write the print function in NumberArray.cpp

void NumberArray::print()
{
  for (int index = 0; index < arraySize; index++)
    cout << aPtr[index] << "  ";
}

It gives me an error

declaration is incompatible with "void NumberArray::print() const

Any thoughts where I might be going wrong on this? The rest of the constructors and class functions work fine.

Upvotes: 2

Views: 304

Answers (1)

Lukas
Lukas

Reputation: 1305

You forgot to add the const qualifier (as well as a semicolon) to the signature of the definition of the function.

You have to do:

void NumberArray::print() const
{
  for (int index = 0; index < arraySize; index++)
    cout << aPtr[index] << "  ";
}

Upvotes: 9

Related Questions