出錯代碼: 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)這一段要加括號什麼的
函數全代碼:
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;//否則返回應交
}