我正在尝试将一个shared_ptrs向量排序到Food对象。 食品类定义为:
class Food {
private:
// Human-readable description of the food, e.g. "all-purpose wheat
// flour". Must be non-empty.
std::string _description;
// Human-readable description of the amount of the food in one
// sample, e.g. "1 cup". Must be non-empty.
std::string _amount;
// Number of grams in one sample; must be non-negative.
int _amount_g;
// Energy, in units of kilocalories (commonly called "calories"), in
// one sample; must be non-negative.
int _kcal;
// Number of grams of protein in one sample; most be non-negative.
int _protein_g;
public:
Food(const std::string& description,
const std::string& amount,
int amount_g,
int kcal,
int protein_g)
: _description(description),
_amount(amount),
_amount_g(amount_g),
_kcal(kcal),
_protein_g(protein_g) {
assert(!description.empty());
assert(!amount.empty());
assert(amount_g >= 0);
assert(kcal >= 0);
assert(protein_g >= 0);
}
const std::string& description() const { return _description; }
const std::string& amount() const { return _amount; }
int amount_g() const { return _amount_g; }
int kcal() const { return _kcal; }
int protein_g() const { return _protein_g; }
};
使用
// Alias for a vector of shared pointers to Food objects.
using FoodVector = std::vector<std::shared_ptr<Food>>;
我的排序算法是:
std::unique_ptr<FoodVector> greedy_max_protein(const FoodVector& foods,
int total_kcal)
{
std::unique_ptr<FoodVector> result(new FoodVector);
int result_cal = 0;
sort(foods.begin(), foods.end(), sortByProtein); //sorting error
...
这里使用sort函数发生错误^^和我的sortByProtein函数是:
bool sortByProtein(const std::shared_ptr<Food>&lhs, const std::shared_ptr<Food>&rhs)
{
return lhs->protein_g() > rhs->protein_g();
}
我一直得到二进制'='没有找到运算符,它采用左手操作数类型'const std :: shared_ptr'或者没有可接受的转换。我已经尝试创建自己的排序函数,但我得到了同样的错误。我需要在我的课程中重载operator =吗?如果是这样,我该怎么做呢?任何帮助将不胜感激!
修改
通过创建新指针来解决问题:
FoodVector *sorted = new FoodVector(foods);
谢谢!
答案 0 :(得分:1)
问题在于尝试排序的函数
std::unique_ptr<FoodVector> greedy_max_protein(const FoodVector& foods, int total_kcal) { std::unique_ptr<FoodVector> result(new FoodVector); int result_cal = 0; sort(foods.begin(), foods.end(), sortByProtein); //sorting error
std::sort()
依赖前两个参数为非const
迭代器 - 即可用于更改它们引用的值。毕竟,如果无法重新分配元素,那么对容器进行排序相当困难。
foods
是对const
(又名FoodVector
)的std::vector<std::shared_ptr<Food> >
引用,因此begin()
和end()
函数为{{1} },并返回类型const
。这不符合std::vector<std::shared_ptr<Food> >::const_iterator
的要求。
解决问题,
std::sort()
的第一个参数中删除const
限定符。请注意,这意味着该函数可能会更改传递的greedy_max_protein()
的元素,并且调用者将无法传递FoodVector
; 答案 1 :(得分:0)
我砍了下来并隔离了问题。 在删除“const”之前,我得到了同样的错误。 但这会编译。
#include <iostream>
//:For: std::vector
#include <vector>
//:For: std::shared_ptr
#include <memory>
//:For: std::sort
#include<algorithm>
class Food{ /* SomeLogicHere */ };
typedef std::vector<std::shared_ptr<Food>> FoodVector;
bool operator==(
std::shared_ptr<Food> lhs,
std::shared_ptr<Food> rhs
){
return true; //TODO: Actual Comparison Logic
}
bool sortByProtein(
std::shared_ptr<Food> lhs,
std::shared_ptr<Food> rhs
) {
//:Dont Care about implementation.
//:Just want minimal example that
//:gets the error.
return false;
}
std::unique_ptr<FoodVector> greedy_max_protein(
FoodVector foods,
int total_kcal
){
sort(foods.begin(),foods.end(),sortByProtein);
}
int main()
{
std::cout<<"Hello World";
return 0;
}