jeudi 20 octobre 2016

divide STL vector by a scalar operator overload

I'm trying to overload the division operator to be able to divide an std::vector by a double scalar with the following:

template <typename T>
std::vector<T> operator/(const double& a, const std::vector<T>& b) {
    std::vector<T> c(b.size());
    for(int i=0;i<b.size();++i)
        c[i] = b[i] / a;
    return c;
}

If I then try to compute:

std::vector<double> M(Z.size());
M = Z / a;

where Z is a valid std::vector.

The compiler (Xcode) stops me saying "Invalid operands to invalid expression ('std::vector' and 'double'). Can anyone tell me why my template does not work and recommend me a suitable alternative, please?

Thanks very much!

Aucun commentaire:

Enregistrer un commentaire