在写小芳便利店v2作业是遇到一个问题
class Basket{
std::vector<Goods> goodlist;
public:
void addGoods(Goods item){
goodlist.push_back(item);
}
void clear(){
goodlist.clear();
}
double getTotalPrice()const{
double total=0.0;
std::vector<Goods>::iterator it;
for(it=goodlist.begin();it!=goodlist.end();it++)
//报错No viable overloaded '='
{
total+=(*it).getPrice();
}
return total;
}
bool isEmpty()const{
return goodlist.empty();
}
std::vector<Goods> GetGoodsList()const{
return goodlist;
}
};
在stackflow上找到解决方案
1.可以将限定函数的const关键字去掉
2.可以将引起错误的goodlist变量限定为mutable
推测是.begin()对vector的操作与指向vector的const指针冲突