错误 C2678:二进制 '=' :未找到采用“const Recipe”类型的左操作数的运算符; (或者没有可接受的转换)
我正在尝试对每个元素中包含一个 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;
};
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Menu::show()
被声明为const
,因此在其内部Menu::recipes
被认为已被声明为std ::矢量<食谱>常量。
显然,对
std::vector<>
进行排序会使其发生变化,因此Menu::show()
不得为const
(或Menu::show()
>Menu::recipes 必须是可变
,但在这种情况下这在语义上似乎不正确)。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).您已将 show 方法标记为 const,但这是不正确的,因为它正在更改菜谱向量。当我编译您使用 gnu gcc 4.2.1 概述的代码时,该错误特定于取消 const 限定符的资格,而不是您发布的错误。
您可以使用关键字
mutable
标记您的向量,但我怀疑这不是您真正想要的?通过将向量标记为可变,它会忽略编译器通常在向量的Menu::show() const
中强制执行的常量,并且每次调用 Menu::show() 时它都会发生更改。如果您确实想使用向量,而不是像其他人建议的那样使用有序集,您可以添加一个脏状态标志,让您的程序知道何时应该使用或不使用。我通过将向量更改为可变来编译以下代码,以向您展示差异,但我仍然建议您不要将 sort from 与 const show 方法一起使用。
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.