简体   繁体   English

错误:&#39;operator *&#39;不匹配(操作数类型是&#39;std :: vector <std::complex<double> &gt;&#39;和&#39;std :: complex <double> “)

[英]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.

相关问题 错误:与“ operator &gt;&gt;”不匹配(操作数类型为“ std :: istream {aka std :: basic_istream <char> }和&#39;std :: vector <double> &#39;) - Error: no match for 'operator>>' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'std::vector<double>') 错误:“ operator =”不匹配(操作数类型为“ std :: map” <int, double> ::迭代器 - error: no match for 'operator=' (operand types are 'std::map<int, double>::iterator 出现错误:'operator&lt;&lt;' 不匹配(操作数类型为 'std::basic_ostream<char> ' 和 'Complex') 尽管重载了 &lt;&lt; 运算符</char> - Getting error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'Complex') despite overloading the << operator 转换 std::vector <complex<double> &gt; 复杂<double> [N] 用于 FFT </double></complex<double> - Convert std::vector<complex<double>> to complex<double>[N] for FFT &quot;+=&quot; 操作在 std::complex 类型之间不起作用<double>和 __complex__ 双 - "+=" operation not working between types std::complex<double> and __complex__ double 铸造 std::complex<double> 作为双 - Casting std::complex<double> as double 错误:'operator+' 不匹配(操作数类型是 'std::vector<int> ' 和 'int')</int> - error: no match for ‘operator+’ (operand types are ‘std::vector<int>’ and ‘int’) 标准::复杂<double>乘以其他类型</double> - std::complex<double> multiplication by other types 错误:“operator+=”不匹配(操作数类型为“long double”和“std::__cxx11::basic_string”<char> &#39;) - error: no match for 'operator+=' (operand types are 'long double' and 'std::__cxx11::basic_string<char>') 使用std :: complex <double> 作为std :: map键 - Using std::complex<double> as a std::map key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM