c++ - 将元素分配给函数类型向量的问题(作为对象的函数)
问题描述
我正在关注本教程,并在其中使用函数作为对象进行介绍。我有以下代码:
double multby2(double num) {
return num * 2;
}
double multby3(double num) {
return num * 3;
}
int main()
{
auto times2 = multby2; //store function as variable
cout << "5 * 2 = " << times2(5) << endl; //so here times2 becomes multby2. I guess an object of it?
vector<function<double(double)>> funcs(2); //vector that takes type dbl func with element size of 2
funcs[0] = multby2(10);
funcs[1] = multby3;
cout << "2 * 10 = " << funcs[0](10) << endl;
}
当我尝试编译funcs[0] = multby2(10);
时,它给了我错误:
Error C2679 binary '=': no operator found which takes a right-hand operand of type 'double' (or there is no acceptable conversion
怎么会这样?我怎么能做,cout << "2 * 10 = " << funcs [0] (10) << endl;
但不能绕过这一步,只是multby(2)
像我试图在 at 那样分配一个参数并将其放入向量中funcs[0] = multby2(10)
。您还会注意到我可以做到,cout << "5 * 2 = " << times2(5) << endl;
我认为我可以遵循相同的想法,只需在向量元素分配中插入要相乘的双精度数。
解决方案
推荐阅读
- python - 我可以使用机器学习模型作为优化问题的目标函数吗?
- css - 为什么我的背景图像会根据屏幕分辨率变大?
- excel - 如何使用 MS Excel 分隔符在有数字的地方拆分单词
- ansible - 如何在ansible中使paramiko CryptographyDeprecationWarnings静音
- python - 带有 Raspberry Pi 的步进电机仅适用于 GPIOZero 的一些延迟时间
- ms-access - 访问:将组合框绑定到记录集,但不允许在更改/更新时更改记录集
- css - 属性值在 css 上的文本阴影处无效
- angular - 如何在指令字段中使用翻译?
- c++ - 数组和它的地址有什么关系?
- android - Android中的ProgressBar只有在完成一些操作后才会显示