类中的参数无效

Mat*_*att 4 c++ arguments class

char* n=m.getName();
Run Code Online (Sandbox Code Playgroud)

Invalid arguments ' Candidates are: char * getName() '上述指令出现以下错误.我错过了什么?

char* Medicine::getName() 
{
    return this->name;
}
Run Code Online (Sandbox Code Playgroud)

name被声明为char name[50];mconst Medicine& m

jua*_*nza 9

如果mconst,则只能const调用方法.也许你可以改变你的方法

const char* Medicine::getName() const; 
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

const char* n=m.getName();
Run Code Online (Sandbox Code Playgroud)

虽然您可能会考虑使用std::string数据成员而不是数组char.

  • 如果有充分理由进行投票,我希望听到他们的意见.我更乐意在此答案中修正错误或在必要时澄清错误. (3认同)