我試圖對包含int和字符串的向量進行排序在每個元素中。它是一個稱爲矢量食譜的類型向量。獲取上述錯誤,這裏是我的代碼:錯誤C2678:二進制'=':找不到操作符找到'const Recipe'類型的左手操作數(或沒有可接受的轉換)
在我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;
};
我做錯了什麼?
顯示我們對您得到這個錯誤行... –
您確定要排序的字符串值,而不是配方ID? –
我加了一個[tag:C++]標籤;它應該引起這個問題更多的關注。 –