在C ++中将常数与复数相乘 [英] Multiplying complex with constant in C++
本文介绍了在C ++中将常数与复数相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码无法编译
#include <iostream>
#include <cmath>
#include <complex>
using namespace std;
int main(void)
{
const double b=3;
complex <double> i(0, 1), comp;
comp = b*i;
comp = 3*i;
return 0;
}
与 错误:"3 *我"中的操作员*"不匹配 这有什么问题,为什么我不能与立即数相乘? b * i有效.
with error: no match for ‘operator*’ in ‘3 * i’ What is wrong here, why cannot I multiply with immediate constants? b*i works.
推荐答案
第一行:
comp = b*i;
编译器调用:
template<class T> complex<T> operator*(const T& val, const complex<T>& rhs);
实例为:
template<> complex<double> operator*(const double& val, const complex<double>& rhs);
在第二种情况下,没有合适的模板int
,因此实例化失败:
In the second case, there is no appropriate template int
, so the instancing fails:
comp = 3.0 * i; // no operator*(int, complex<double>)
这篇关于在C ++中将常数与复数相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文