title: C++ sorting a vector of shared_ptrs
body: <p>I'm trying to sort a vector of shared_ptrs to Food objects.
  Food class is defined as:</p>
  
  <pre><code>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&amp; description,
      const std::string&amp; 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 &gt;= 0);
      assert(kcal &gt;= 0);
      assert(protein_g &gt;= 0);
  
  }
  const std::string&amp; description() const { return _description; }
  const std::string&amp; amount() const { return _amount; }
  int amount_g() const { return _amount_g; }
  int kcal() const { return _kcal; }
  int protein_g() const { return _protein_g; }
  
  };
  </code></pre>
  
  <p>using</p>
  
  <pre><code>// Alias for a vector of shared pointers to Food objects.
  using FoodVector = std::vector&lt;std::shared_ptr&lt;Food&gt;&gt;;
  </code></pre>
  
  <p>My sorting algorithm is:</p>
  
  <pre><code>std::unique_ptr&lt;FoodVector&gt; greedy_max_protein(const FoodVector&amp; foods,
  int total_kcal) 
  {
  std::unique_ptr&lt;FoodVector&gt; result(new FoodVector); 
  int result_cal = 0;                             
  sort(foods.begin(), foods.end(), sortByProtein); //sorting error
  ...
  </code></pre>
  
  <p>The error is occuring with the sort function here ^^ and my sortByProtein function is:</p>
  
  <pre><code>bool sortByProtein(const std::shared_ptr&lt;Food&gt;&amp;lhs, const std::shared_ptr&lt;Food&gt;&amp;rhs)
  {
  return lhs-&gt;protein_g() &gt; rhs-&gt;protein_g();
  
  }
  </code></pre>
  
  <p>I keep getting the binary'='no operator found which takes a left hand operand type 'const std::shared_ptr' or there is no acceptable conversion. I've tried creating my own sort function but i get the same error. Do i need to overload operator= in my class ? If so how do i go about doing that? Any help would be greatly appreciated! </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>fixed the issue by creating a new pointer:</p>
  
  <pre><code>FoodVector *sorted = new FoodVector(foods);
  </code></pre>
  
  <p>Thanks!</p>