[英]Object has type qualifiers that are not compatible with the member function
[英]IntelliSense: the object has type qualifiers that are not compatible with the member function
我有一个名为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像print
一样。 您不能使用const对象调用非const方法。 由于您的Person对象是您的其他类的直接成员,并且由于您使用的是const方法,因此它们被视为const。
一般来说,你应该考虑你编写的每个方法,并将其声明为const,如果它是什么。 像getScore
和getName
这样的简单getter应该始终是const。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.