gpt4 book ai didi

G++ 4.5 中 std::complex 的 C++11 复制赋值 - 与 'operator+' 不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:11 25 4
gpt4 key购买 nike

下面的代码无法使用 -std=c++0x 开关在 g++ 4.5.0 版中编译。我收到以下错误消息:

error: no match for 'operator+' in 'std::pow [with _Tp = float, _Up = int, typename __gnu_cxx::__promote_2<_Tp, _Up>::__type = double](((const std::complex<float>&)((const std::complex<float>*)(& x))), ((const int&)((const int*)(&2)))) + y'

我相信这与提到的可分配要求有关here .我应该为 complex 定义自己的复制赋值运算符吗?如果是,怎么办?

#include <complex>
using namespace std;

int main(int argc, char *argv[]) {
complex<float> x,y;
x = pow(x,2); // ok
x = x + y; // ok
x = pow(x,2) + y; // error
return 0;
}

最佳答案

[cmplx.over]/p3 为 pow 指定额外的重载什么时候complex涉及:

Function template pow shall have additional overloads sufficient to ensure, for a call with at least one argument of type complex<T>:

  1. If either argument has type complex<long double> or type long double, then both arguments are effectively cast to complex<long
    double>
    .

  2. Otherwise, if either argument has type complex<double>, double, or an integer type, then both arguments are effectively cast to complex<double>.

  3. Otherwise, if either argument has type complex<float> or float, then both arguments are effectively cast to complex<float>.

2 被提升为 double ,并且 pow(complex<float>, double)返回 complex<double> .

关于G++ 4.5 中 std::complex 的 C++11 复制赋值 - 与 'operator+' 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8478338/

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