不相容的类声明c ++

Pod*_*odo 2 c++ class declaration

我上课NumberArrayNumberArray.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);
};
Run Code Online (Sandbox Code Playgroud)

当我去NumberArray.cpp中编写print函数时

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

它给了我一个错误

声明与"void NumberArray :: print()const不兼容"

有什么想法,我可能会出错吗?其余的构造函数和类函数工作正常.

小智 9

您忘记将const限定符(以及分号)添加到函数定义的签名中.

你必须做:

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