A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
std::vector<float> does not have an overload of operator= that takes a single float. That is why the compiler reports it cannot find an = operator for assigning a vector from a scalar expression.
From the description, chm, vec1, and vec2 are std::vector<float> and cmid is float. Then:
chm[i] = vec1[i] + (float cmid * vec2[i]) / 2.;
operates on individual elements, so the type of the right-hand side is float, and chm[i] is also a float reference. This form is valid and should compile, because it uses the built‑in operator= for float.
The error typically appears when trying to assign the whole vector from a scalar expression, for example:
chm = vec1 + (cmid * vec2) / 2.0f; // invalid for std::vector<float>
std::vector<float> does not define operator+, operator*, or operator/ between vectors and scalars, so the compiler cannot find a suitable overloaded operator (C2545 / C2446 style issues).
To fix this, either:
- Work element‑wise (correct for
std::vector<float>):for (size_t i = 0; i < 3; ++i) { chm[i] = vec1[i] + (cmid * vec2[i]) / 2.0f; } - Or, if
chm,vec1,vec2are custom vector types (for example a 3D vector class), ensure that:- There is an
operator=that assigns from the same vector type. - There are overloaded operators such as
operator+,operator*(scalar * vector), andoperator/(vector / scalar) with appropriate parameter types, as indicated by the error about not finding an overloaded operator.
- There is an
If using a custom 3D vector type similar to CVEC3D, the class must provide the necessary arithmetic operators in addition to compound assignments like operator+= and operator-= that are already defined.
References: