不兼容的类声明c ++ [英] Incompatible Class Declaration c++

查看:68
本文介绍了不兼容的类声明c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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);
};

当我在NumberArray.cpp中编写打印功能时

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

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

这给我一个错误

声明与"void NumberArray :: print()const

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.

推荐答案

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

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

您必须这样做:

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

这篇关于不兼容的类声明c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆