Lambda表达“无法绑定”

时间:2016-01-09 20:01:07

标签: c++ lambda

所以基本上我正在尝试使用lambdas来获得农场中最重的动物。但是我被困住了,我真的没有办法解决这个问题。

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

我有一个Animal类

animal.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

animal.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;
  }

和一个农场班

farm.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

Farm.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();
    });

  }

的main.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;
             ^

我似乎无法找到如何解决这个问题。

编辑(解决方案)

Animal.h

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

Animal.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;
  }

Main.cpp的

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

Farm.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)'

1 个答案:

答案 0 :(得分:1)

  

这个答案是完成,因为它解决了前两个草稿   问题但未能解决最终草案..

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

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

编辑问题后:

overload pointer Animal object,但您尝试传递Animal,因此请将重载修改为Animal const&或更好{{} {1}}而不是Animal*或将结果的引用传递给std::cout。当然,第一种解决方案更适合IMO。