c – 智能感知:对象具有与成员函数不兼容的类型限定符

c – 智能感知:对象具有与成员函数不兼容的类型限定符,第1张

概述我有一个类叫做Person: class Person { string name; long score;public: Person(string name="", long score=0); void setName(string name); void setScore(long score); string getName(); 我有一个类叫做Person:
class Person {    string name;    long score;public:    Person(string name="",long score=0);    voID setname(string name);    voID setscore(long score);    string getname();    long getscore();};

在另一个类中,我有这个方法:

voID print() const {     for (int i=0; i< nPlayers; i++)        cout << "#" << i << ": " << people[i].getscore()//people is an array of person objects    << " " << people[i].getname() << endl;}

这是人的宣言:

static const int size=8;     Person people[size];

当我尝试编译它,我得到这个错误:

IntelliSense: the object has type qualifIErs that are not compatible with the member function

在2人[i]的打印方式下的红线

我究竟做错了什么?

解决方法 getname不是const,getscore不是const,而是print.使第一个两个const像打印.您不能使用const对象调用非const方法.因为你的Person对象是你的另一个类的直接成员,并且由于你是一个const方法,所以它们被认为是const.

一般来说,你应该考虑你写的每个方法,并声明它是const,如果这是它是什么. getscore和getname等简单的getter应该是const.

总结

以上是内存溢出为你收集整理的c – 智能感知:对象具有与成员函数不兼容的类型限定符全部内容,希望文章能够帮你解决c – 智能感知:对象具有与成员函数不兼容的类型限定符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1234333.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存