迭代器上的C ++ Discard限定符(const)

时间:2014-04-27 01:12:19

标签: c++ list iterator const

我遇到了迭代器的问题。当我编译我的项目时,我不断收到此错误。

Kitchen.cpp: In member function ‘void Kitchen::If_Cook_Ok(const Order&) const’:
    Kitchen.cpp:45:33: error: passing ‘const Order’ as ‘this’ argument of ‘std::list<IngredType::Ingredient> Order::getIngredient()’ discards qualifiers [-fpermissive]
    Kitchen.cpp:45:70: error: passing ‘const Order’ as ‘this’ argument of ‘std::list<IngredType::Ingredient> Order::getIngredient()’ discards qualifiers [-fpermissive]

我已经尝试将constness放在函数成员上,但我不断收到此错误。这是代码

Order类具有成员变量,getter getIngredients()返回std :: list女巫

void    Kitchen::If_Cook_Ok(const Order &order) const
{
  std::cout << "congratulations you barely made it" << std::endl;
  std::list<IngredType::Ingredient>::const_iterator it;

  for (it = order.getIngredient().begin(); it != order.getIngredient().end(); ++it)
    {
      std::cout << *it << std::endl;
    }
}

非常感谢帮助。

2 个答案:

答案 0 :(得分:3)

orderconst Order&。因此,您只能调用Order类的const方法。似乎Order::getIngredient()不是常量。

答案 1 :(得分:2)

方法Order中的If_Cook_Ok参数为const,因此您只能在此对象上调用cons t方法。也许getIngredients()方法不是const。尝试在该方法上添加const