加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11中的std :: complex的C 11复制赋值 – 与’operator’不匹配

发布时间:2020-12-16 03:28:37 所属栏目:百科 来源:网络整理
导读:下面的代码无法使用-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::complexfloat)((cons
下面的代码无法使用-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提到的可分配要求有关.我应该为复杂定义我自己的复制赋值运算符吗?如果是这样,怎么样?

#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的额外重载:

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)返回复数< double>.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读