Lambda 表达式"无法绑定"

Lambda expression "cannot bind'

本文关键字:绑定 Lambda 表达式      更新时间:2023-10-16

所以基本上我试图用lambdas来获得农场里最重的动物。但是我被困住了,我真的不知道如何解决这个问题。

调用方法killHeaviestAnimalOnFarm();并打印它时出现问题。(见main.cpp(

我有一个动物类

动物.H

#ifndef ANIMAL_H
#define ANIMAL_H
#include <string>
#include <iostream>
#include <vector>
#include <memory>
class Animal
  {
  public:
    Animal(std::string aType, const char & anSex, float aWeight);
    std::string getType() const {return type;}
    char getSex() const {return sex;};
    float getWeight() const {return weight;};
    void setWeight(float value) {weight = value;};
    void feed(int amount);
    Animal breed(Animal &aFather);
  private:
    const std::string type;
    const char sex;
    float weight;
  };
std::ostream & operator <<(std::ostream & os, Animal *an);
#endif // ANIMAL_H

动物.cpp

#include "animal.h"
#include "farm.h"
Animal::Animal(std::string aType, const char &aSex, float aWeight) :
  type{aType}, sex{aSex}, weight{aWeight}
  {
  }
void Animal::feed(int amount)
  {
  if (type == "sheep")
    weight += amount * 0.02f;
  else
    if (type == "cow")
      weight += amount * 0.05f;
  }
Animal Animal::breed(Animal &aFather)
  {
  float startWeight = (type == "cow") ? 35.0f : 6.5f;
  if (aFather.getType() == type && aFather.getSex() == 'M')
  return Animal(type, 'F', startWeight);
  }
std::ostream & operator <<(std::ostream & os, Animal *an)
  {
  std::string animal = an->getType();
  animal += "(";
  animal += an->getSex();
  animal +=  ")";
  os << animal <<  ", current weight = " << an->getWeight() << std::endl;
  return os;
  }

和农场班

农场.h

#ifndef FARM_H
#define FARM_H
#include <string>
#include <vector>
#include <memory>
#include "animal.h"
class Farm
  {
  public:
    Farm(std::string farmName);
    std::string getName() const {return name;};
    void addAnimal(Animal &newAnimal);
    Animal killHeaviestAnimalOnFarm();
  private:
    const std::string name;
    std::vector<Animal> animals;
  };
#endif // FARM_H

农场.cpp

#include "farm.h"
#include <algorithm>
#include <iostream>

Farm::Farm(std::string farmName):name{farmName}
  {
  }
void Farm::addAnimal(Animal &newAnimal)
  {
  animals.push_back(newAnimal);
  }

Animal Farm::killHeaviestAnimalOnFarm()
  {
  std::max_element(animals.begin(), animals.end(), [] (Animal first, Animal second)
  {
    return first.getWeight() < second.getWeight();
    });
  }

主.cpp

#include <iostream>
#include "farm.h"
#include <memory>
int main()
  {
  Farm meuhBoeh("Tradition in Technology");
  Animal * eva = new Animal("sheep", 'F', 35.2f);
  Animal * adam = new Animal("sheep", 'M', 45.6f);
  Animal * bella = new Animal("cow", 'F', 256.3f);
  Animal * taurus = new Animal("cow", 'F', 343.8f);
  meuhBoeh.addAnimal(*eva);
  meuhBoeh.addAnimal(*adam);
  meuhBoeh.addAnimal(*bella);
  meuhBoeh.addAnimal(*taurus);
  std::cout << "Animal slaughtered = " << meuhBoeh.killHeaviestAnimalOnFarm() << std::endl;
  }

我收到以下错误:

/main.cpp:17: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
   std::cout << "Animal slaughtered = " << meuhBoeh.killHeaviestAnimalOnFarm() << std::endl;
             ^

我似乎找不到解决这个问题的方法。

编辑(解决方案(

动物.h

std::ostream & operator <<(std::ostream & os,const Animal &an);

动物.cpp

更新的运算符

std::ostream & operator <<(std::ostream & os, const Animal &an)
  {
  std::string animal = an.getType();
  animal += "(";
  animal += an.getSex();
  animal +=  ")";
  os << animal <<  ", current weight = " << an.getWeight() << std::endl;
  return os;
  }

主.cpp

 std::cout << "Animal slaughtered = " << meuhBoeh.killHeaviestAnimalOnFarm() << std::endl;

农场.cpp

(

也尝试使用(动物第一,动物第二(,但同样的错误(

Animal Farm::killHeaviestAnimalOnFarm()
  {
  std::max_element(animals.begin(), animals.end(), [] (Animal &first, Animal &second)
  {
    return first.getWeight() < second.getWeight();
    });
  }

错误:

main.cpp:33: error: undefined reference to `operator<<(std::ostream&, Animal)'

这个答案是不完整的,因为它解决了前两个草案 问题,但未能解决最终草案。

killHeaviestAnimalOnFarm返回Animal而不是string或运算符<<的任何重载。我想你想要这样的东西;

auto killed=meuhBoeh.killHeaviestAnimalOnFarm();
std::cout << "Animal slaughtered = " << killed.name << std::endl;

问题编辑后:

您的overload采用Animal pointer,但您正在尝试传递object因此,要么将重载编辑为仅Animal或更好的Animal const&而不是Animal*,要么将结果的引用传递给std::cout。当然,第一个解决方案是IMO更可取的。