出错代码: if (year == p->YearAndMonth.substr(0, 4)) {
FeePaid += p->FeeSum;
FeePayable += p->Fee;
}
year是string类(2016),p是指向一个结构体的指针,YearAndMonth是结构体里的成员变量,也是string类(201601)。
我想要截取YearAndMonth的前四个字符和year比看是否一样,一样就执行累加运算(计算一年的物业账单费用总合)
不知道那里错了。是不是p->YearAndMonth.substr(0, 4)这一段要加括号什么的

C++的string类的substr函数发生错误(新手)
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- wumeizhi1106 2016-11-09 08:07关注
函数全代码:
int People::Count(string year,int n) {
List *p=PHead;
int FeePaid=0;//业主年度已交
int FeePayable=0;//业主年度应交
while (p!= NULL)
{
if (year == p->YearAndMonth.substr(0, 4)) {
FeePaid += p->FeeSum;
FeePayable += p->Fee;
}
p = p->Pnext;
}
if (n == 1) return FeePaid;//接收1返回已交
else return FeePayable;//否则返回应交
}本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报