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

时间:2017-09-04 13:06:35

标签: c++ vector complex-numbers

我使用带有复数的向量类,我必须将复数向量与复数相乘,如下所示:

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>’)

是否可以使用这些参数重载operator *?

1 个答案:

答案 0 :(得分:3)

是的,这是可能的。以下是为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; } 的方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View currentFocus = ((Activity)mContext).getCurrentFocus();
    if (currentFocus != null) {
        currentFocus.clearFocus();
    }  
}