Hakeem El Bakka-lee
Hakeem El Bakka-lee

Reputation: 766

C++ Discard qualifiers on iterator (const)

I got a problem with a iterator. When i compile my project i keep getting this error.

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]

I already tried to put constness on the function member, but i keep getting this error. Here is the code

The Order class has as member variable a std::list witch is returned by the getter getIngredients()

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;
    }
}

Help will be much appreciated.

Upvotes: 1

Views: 1309

Answers (2)

Rakib
Rakib

Reputation: 7625

TheOrder parameter in the method If_Cook_Ok is const, so you can only call const methods on this object. Perhaps the getIngredients() method is not const. Try adding const on that method.

Upvotes: 3

AntiClimacus
AntiClimacus

Reputation: 1420

order is a const Order&. Thus, you could only call const methods of Order class. And it appears that Order::getIngredient() is not const.

Upvotes: 4

Related Questions