C ++ no match for 'operator + =' (operand types are 'float' and 'std :: chrono :: durationlong int')


I am trying to calculate the average execution time of a program, for 10 executions, for this I use the library chrono of C ++ 11, however, I'm getting an error that says there is no operator to make += , I understand that variables declared as auto can be matched with other types of data.


int i=0;
float execution_time=0;
   auto start = chrono::system_clock::now();

   coche_.A_star(map_);//para llamar a A_star,asi se implementan constructores

   auto end = chrono::system_clock::now();

   auto elapsed = chrono::duration_cast<std::chrono::seconds>(end - start);

   execution_time += elapsed; //ERROR

cout << "tiempo de ejecucion" << execution_time/10 <<endl;


error: no match for ‘operator+=’ (operand types are ‘float’ and ‘std::chrono::duration<long int>’)
                     execution_time += elapsed;

The code is part of a larger function, I only put what is relevant to understand the doubt. I have declared using namespace std .

Thanks, thanks.

asked by AER 22.11.2018 в 22:14

2 answers


I understand that variables declared as auto can be matched with other types of data.

Well, you got it wrong. The feature you describe is common in weak typing languages, but C ++ is not one of them.

C ++ is a language of strong typing , this implies that variables have only one type of data, that their type does not vary in runtime and that its type is defined at the time of definition of the variable.

Thus, auto deduces the type of the variable based on its initializer:

auto x = 42;      // x es int
auto y = 42u;     // y es unsigned int
auto z = 42f;     // z es float
auto t = .42;     // t es double
auto u = 42l;     // u es long
auto v = 42ull;   // u es unsigned long long
auto w = new int; // w es int *
auto e = 'a' + 0b0001 + .0f; // e es float porque la expresión completa es float

Therefore, since the return of chrono::duration_cast is a duration, auto deducts std::chrono::duration and a float can not add that data, you get the error.

Apart from the solution provided by Neoniet , you could define the operator that you need:

template <typename T>
float &operator +=(float &f, const std::chrono::duration<T> &d)
    return (f += d.count());
answered by 23.11.2018 / 12:04

I would do something like that.

#include <iostream>
#include <chrono>

using namespace std::chrono;

int main(int argc, char* argv[])
        int i=0;
        std::chrono::duration<float,std::milli> duracion,tiempo_ejecucion;

                auto inicio = high_resolution_clock::now();
                // lo que sea que haya que medir
                auto fin = high_resolution_clock::now();
                duracion = (fin - inicio);

                tiempo_ejecucion += duracion; 

        std::cout << "tiempo de ejecucion" << (tiempo_ejecucion.count()/10) << std::endl;
        return 0;

That I know auto implies that the type of variable is that of its initializer ... but nothing else.

answered by 22.11.2018 в 23:51