我正在尝试对每个元素中包含一个 int 和一个字符串的向量进行排序。它是一个类类型的向量,称为向量食谱。出现上述错误,这是我的代码:
在我的 Recipe.h 文件中
struct Recipe {
public:
string get_cname() const
{
return chef_name;
}
private:
int recipe_id;
string chef_name;
在我的 Menu.cpp 文件中
void Menu::show() const {
sort(recipes.begin(), recipes.end(), Sort_by_cname());
}
在我的 Menu.h 文件中
#include <vector>
#include "Recipe.h"
using namespace std;
struct Sort_by_cname
{
bool operator()(const Recipe& a, const Recipe& b)
{
return a.get_cname() < b.get_cname();
}
};
class Menu {
public:
void show() const;
private
vector<Recipe> recipes;
};
我究竟做错了什么?
Menu::show()
被宣布const
,所以在它里面Menu::recipes
被认为已被声明为std::vector<Recipe> const
.
显然,排序astd::vector<>
改变它,所以Menu::show()
必须不const
(or Menu::recipes
必须是mutable
,但在这种情况下这在语义上似乎不正确)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)