Incompatible Class Declaration c++

会有一股神秘感。 提交于 2020-01-04 06:35:14

问题


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.


回答1:


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] << "  ";
}


来源:https://stackoverflow.com/questions/36729253/incompatible-class-declaration-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!