我需要迭代一套。该集合是一组Items,它是我实现的类。我有这个错误:
error C2662: 'double Item::getUnitPrice(void)' : cannot convert 'this' pointer from 'const Item' to 'Item &'
这是我遍历集合的函数,但我有错误 " sum + = it-> getUnitPrice();"
double Customer::totalSum()
{
double sum = 0;
set<Item>::iterator it;
for (it = _items.begin(); it != _items.end(); ++it)
{
sum += it->getUnitPrice();
}
return sum;
}
提前致谢!
答案 0 :(得分:0)
你应该遍历你的集合:
double Customer::totalSum()
{
double sum = 0f;
for( std::set<Item>::iterator it = _items.begin(); it != _items.end(); ++it )
{
sum += (*it).totalPrice();
}
return sum;
}
答案 1 :(得分:0)
我只是不知道如何使用套装到达unitPrice。
double Customer::totalSum()
{
double sum = 0;
set<Item>::iterator begin = _items.begin(); <<< Although clever compilers can make
set<Item>::iterator end = _items.end(); << this a futile attempt at optimization.
for( set<Item>::iterator iter = begin; iter != end; ++iter )
{
sum += it->getUnitPrice();
}
return sum;
}