[英]C++ Discard qualifiers on 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類具有一個作為成員變量的std :: list巫婆,由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;
}
}
幫助將不勝感激。
order
是const Order&
。 因此,您只能調用Order
類的const方法。 似乎Order::getIngredient()
不是const。
該Order
的方法參數If_Cook_Ok
是const
,所以你只能叫cons
這個對象在T方法。 也許getIngredients()
方法不是const
。 嘗試在該方法上添加const
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.