Lambda 表达式"无法绑定"
Lambda expression "cannot bind'
所以基本上我试图用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更可取的。
相关文章:
- 传递给 lambda C++绑定函数
- 获取具有静态局部变量的绑定/推断捕获 lambda 的函数指针
- 在 c++ 中绑定 lambda
- 结构化绑定语法是否可以在多态 lambda 中使用
- 重构嵌套绑定以使用 lambda 函数
- 解除 lambda 事件处理程序的绑定
- std::绑定到 lambda: 编译错误
- 使用带有增强兽的 lambda 而不是绑定
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 为什么 lambda 表达式的捕获列表无法使用结构化绑定分解
- std :: async使用绑定到lambda的rvalue引用
- 在c++11中重新绑定lambda.有可能吗
- C++将此指针与 lambda 函数中的绑定一起使用
- Lambda 表达式"无法绑定"
- MSVC和boost::lambda::绑定错误:T0:不允许使用标准参数
- boost绑定或返回常量的lambda函数
- 从绑定到lambda函数中获取运算符()的类型
- c++14用于函数绑定的可变lambda捕获
- std::bind():将lambda与右值引用作为参数绑定
- 是否可以绑定lambda函数的第二个参数