3

当我在下面的代码中调用 getCount 函数时,QT 4.7.3 编译器给出了错误。构建错误

将 'cont Person' 作为 'int Person::getCount(const QString&) 的 'this' 参数传递会丢弃限定符

 bool Person::IsEligible(const QString& name)
 {
      int count = 0;
      count = getCount(name);
 }

 int Person::getCount(const QString& name)
 {
  int k =0
  return k;
 }
4

1 回答 1

3

错误不是传递字符串参数的问题,而是你有一个const人,例如:

const Person p1;
Person p2;
p1.IsEligible("whatever"); //Error   
p2.IsEligible("whatever"); //Fine because p2 isn't const

如果IsEligible意味着可以在 s 上调用,const Person那么您可以说:

bool Person::IsEligible(const QString& name) const
 {
      int count = 0;
      count = getCount(name);
 }

(并更改您没有显示得太明显的相应声明),但我不能 100% 确定您打算这样做。

于 2012-04-23T18:53:07.680 回答