问题
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