[英]Error: no match for ‘operator*’ (operand types are ‘std::vector<std::complex<double> >’ and ‘std::complex<double>’)
I use the vector class with complex numbers and I have to multiply a vector of complex numbers with a complex number, like this: 我使用带有复数的向量类,我必须将复数向量与复数相乘,如下所示:
vector< complex<double> > vec;
complex<double> z;
// some initialization of vec and z ...
vector< complex<double> > res;
res = vec*z; // here is the error
error: no match for 'operator*' (operand types are 'std::vector<std::complex<double> >' and 'std::complex<double>')
Is it possible to overload operator* with these parameters? 是否可以使用这些参数重载operator *?
Yes, this is possible. 是的,这是可能的。 Here is how you can implement your own operator
*
for complex<T>
: 以下是如何为
complex<T>
实现自己的运算符*
:
template <typename T>
vector<complex<T>> operator *(const vector<complex<T>>& v, const complex<T> z) {
vector<complex<T>> res;
transform(v.begin(), v.end(), back_inserter(res),
[&](complex<T> x) -> complex<T> {
return x * z;
});
return res;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.