gpt4 book ai didi

c++ - 在 std::vector 中存储函数在 linux 上可以正常工作,但甚至不能在 windows 上编译

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:34 25 4
gpt4 key购买 nike

此程序将函数存储在 std::vector 中,适用于 Linux 上的 g++ 7.2.0,但无法在 Windows 上使用 visual c++ 2017 v15.5.4 进行编译错误是 vector 中的 sin()cos()tan():

E0289 cannot determine which instance of overloaded function "sin" is intended. 

我不知道如何修改它以便它也适用于 Windows。

#include <vector>
#include <cmath>
#include <functional>
#include <iostream>

typedef std::function<double(double)> AFunc;

std::vector<AFunc> funcs = {
sin,
cos,
tan,
[](double x) { return x*x; },
};

int main()
{
std::cout << funcs[0](2) << std::endl;
std::cout << funcs[1](2) << std::endl;
std::cout << funcs[2](2) << std::endl;
std::cout << funcs[3](2) << std::endl;

}

最佳答案

sincostan 是在多个重载中提供的(针对各种浮点类型),因此仅仅引用它们的名称并不是'不足以准确解决您所指的功能。您可以使用 @max66 答案中的转换来解决歧义,或者只使用 lambda,这是 IMO 更清晰的语法和更少的输入:

std::vector<AFunc> funcs = {
[](double x) { return sin(x); },
[](double x) { return cos(x); },
[](double x) { return tan(x); },
[](double x) { return x*x; },
};

关于c++ - 在 std::vector 中存储函数在 linux 上可以正常工作,但甚至不能在 windows 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48367026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com