C++相同的命名空间问题
C++ same namespace issue
<iostream>
和<cmath>
中有std
命名空间。它具有相同的功能,名为sinh
,等等。但它与参数和返回类型不同。
这是代码。
#include <iostream>
#include <cmath>
#include <functional>
#include <vector>
typedef std::function<double(double)> HyperbolicFn;
std::vector<HyperbolicFn> fns = {
std::sinh, std::cosh, std::tanh
};
auto main(void) -> int {
return 0;
}
我编译了它。
$ clang -c 测试.cpp
编译器消息如下所示。
test.cpp:8:27: error: no matching constructor for initialization of 'std::vector<HyperbolicFn>'
(aka 'vector<function<double (double)> >')
std::vector<HyperbolicFn> fns = {
^ ~
<cmath>
标头包含double sinh(double)
函数。但<iostream>
(<complex>
)没有。
我该如何解决这个问题?我想将此代码与标头中的函数一起使用<cmath>
。
std::sinh
和其他的都是重载的,std::function
不能很好地处理重载,它无法区分它们。您可以进行显式强制转换
using Hyper = double(*)(double);
std::vector<HyperbolicFn> fns = {
static_cast<Hyper>(std::sinh),
static_cast<Hyper>(std::cosh),
static_cast<Hyper>(std::tanh)
};
或者改用 lambda
std::vector<HyperbolicFn> fns = {
[](double a) { return std::sinh(a); },
[](double a) { return std::cosh(a); },
[](double a) { return std::tanh(a); }
};
相关文章:
- 有关命名空间和模板参数的名称的问题
- 命名空间范围问题
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 从命名空间嵌入资源的问题(C++和CMake)
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- 关于"使用命名空间标准"的问题?
- 更改命名空间以自定义 Boost XML 的标记名称后的反序列化问题
- 有关通过派生类在命名空间内继承的问题
- C++相同的命名空间问题
- C++11 中的命名空间问题?
- C++中的编译错误可能是命名空间问题
- 命名空间问题:前向声明和混合命名空间
- C++命名空间问题
- LLVM设置函数传递的问题-命名空间问题
- C++中的命名空间问题
- 如何解决此命名空间问题
- C++编译器错误;我想是命名空间问题
- c++ python构造函数类型错误(可能的命名空间问题)
- 如何避免juice框架和OpenCV之间的干扰命名空间问题
- 枚举的命名空间问题