是否可以使用C ++中的[]运算符访问复数的实部和虚部 [英] Is it possible to access the real and imaginary parts of a complex number with the [] operator in C++

查看:157
本文介绍了是否可以使用C ++中的[]运算符访问复数的实部和虚部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C ++中的< complex> 库。是否可以使用[]运算符访问复数的实部和复数部分?我想使用 myComplexNum [0] 而不是 myComplexNum.real() myComplexNum [ 1] 而不是 myComplexNum.imag()



使用MingW 4.5.2,我收到错误:没有找到与operator []'匹配。我真的需要使用[]运算符现在,因为否则我将不得不改变几百行代码。

解决方案

没有。



您可以从 std :: complex<> 并继承构造函数并添加一个重写的 operator [] ,但我建议反对。 >

I'm using the <complex> library in C++. Is it possible to access the real and complex parts of a complex number using the [] operator? I want to use myComplexNum[0] instead of myComplexNum.real() and myComplexNum[1] instead of myComplexNum.imag().

I tried to do it using MingW 4.5.2, and I receive the error: no match found for 'operator[]'. I really need to use the [] operator for now, because otherwise I would have to change hundreds of lines of code.

解决方案

Nope.

You could derive from std::complex<> and inherit constructors and add an overridden operator[], but I would advise against it.

这篇关于是否可以使用C ++中的[]运算符访问复数的实部和虚部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆