I'm trying to sort a vector that contains an int and a string in each element. It is a vector of class type called vector recipes. Getting the above error, here's my code:
In my Recipe.h file
struct Recipe {
public:
string get_cname() const
{
return chef_name;
}
private:
int recipe_id;
string chef_name;
In my Menu.cpp file
void Menu::show() const {
sort(recipes.begin(), recipes.end(), Sort_by_cname());
}
In my Menu.h file
#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;
};
What am I doing wrong?
Menu::show()
is declaredconst
, so inside of itMenu::recipes
is considered to have been declared asstd::vector<Recipe> const
.Obviously, sorting a
std::vector<>
mutates it, soMenu::show()
must not beconst
(orMenu::recipes
must bemutable
, but this seems semantically incorrect in this case).You have marked your show method as
const
which isn't true because it is changing the recipes vector. When I compile the code you have outlined with gnu gcc 4.2.1 the error is specific to disqualifying the const qualifier, not the error you've posted.You could mark your vector with the keyword
mutable
but I suspect that isn't what you really want? By marking the vector mutable it ignores the constness the compiler would normally enforce withinMenu::show() const
of the vector and it gets changed everytime Menu::show() is called. If you really want to use the vector, and not an ordered set like others have suggested, you could add a dirty state flag to let your program know when it should resort, or not.The following code I have compiles by changing the vector to mutable to show you the difference, but I still recommend that you don't use sort from with a const show method.