-2

I want to obtain the average of some gyro readings and it involves dividing an std::vector<double> type with a double type, but I get the following error that reports

Invalid operands to binary expression ('std::vector' and 'double')

How can I resolve this?


    double n_readings;
    std::vector<double> gyro_reading;
    for(int i = 0; i < n_readings; i++) {

        gyro_reading.push_back(gyro_z());
        msleep(1);

    }

    double average = gyro_reading/n_readings;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
OoRoOrOoRoO
  • 69
  • 10
  • 1
    You can't divide a vector by a number. To get the average, sum up the elements of the vector, then divide by the size of the vector. Check this question to see ho to do that: https://stackoverflow.com/questions/3221812/how-to-sum-up-elements-of-a-c-vector – Blaze Jun 17 '19 at 12:50
  • 5
    `double average = std::accumulate(std::begin(gyro_reading),std::end(gyro_reading), 0.0) / n_readings`. Will require the `algorithm` header. – Mansoor Jun 17 '19 at 12:52
  • 1
    `std::valarray` supports member-wise operations directly, for `std::vector` you have to use loops or algorithms – 463035818_is_not_an_ai Jun 17 '19 at 13:03
  • 1
    Why the downvotes? This is a perfectly valid question. @Mansoor You should post your comment as an answer, preferably with a short explanation as to why OPs attempt doesn't work. – Max Vollmer Jun 17 '19 at 13:05
  • @MaxVollmer: It lacks a compilable example, and is incomplete. E.g. what is the return type of `gyro_z()`? – Bathsheba Jun 17 '19 at 13:12
  • 3
    I am an absolute fan of [MCVE]s, but come on. OPs error message is clear enough. And even if that's the reason, I see 4 downvotes but only one vote to close because it lacks a [MCVE]. How is OP supposed to improve their question that way. – Max Vollmer Jun 17 '19 at 13:15

1 Answers1

5

Your call to gyro_reading/n_readings requires a / operator to be defined between types std::vector<T> and T. Standard vector does not have such an operator. Even if it did, the result would probably be an elementwise divide rather than a sum reduction then divide.

The following:

#include <iterator>
#include <numeric>
//...
double average = std::accumulate(std::begin(gyro_reading),std::end(gyro_reading), 0.0) / n_readings;

will accumulate all of the elements gyro_reading, the reduction is done using the operator+ by default, then divide by n_readings. Also, you could use gyro_reading.size() instead of n_readings as this will always be consistent.

Mansoor
  • 2,357
  • 1
  • 17
  • 27
  • How does the sum part differ from ```std::accumulate(gyro_reading.begin(), gyro_reading.end(), 0.0)```? Would it lead to the same result? – OoRoOrOoRoO Jun 17 '19 at 18:10
  • 1
    If `gyro_reading` is a `std::vector`, then there is no distinction. The free functions are more generic and will work with C-arrays, see this [post](https://stackoverflow.com/questions/8452130/when-to-use-stdbegin-and-stdend-instead-of-container-specific-versions). – Mansoor Jun 17 '19 at 22:45