I came across a weird error with std::chrono, If I do something like this:
TimeHandling time(std::chrono::milliseconds(1000 / 125));
time.start();
Everything is ok. But if instead I put the milliseconds value in a variable:
int mpl = 1000 / 125;
TimeHandling time(std::chrono::milliseconds(mpl));
time.start();
g++ throw this error:
request for member ‘start’ in ‘time’, which is of non-class type ‘TimeHandling(std::chrono::milliseconds) {aka TimeHandling(std::chrono::duration<long int, std::ratio<1l, 1000l> >)}’
Does someone know why ?
The second version declares a function with a parameter called mpl
See http://en.wikipedia.org/wiki/Most_vexing_parse and https://stackoverflow.com/tags/most-vexing-parse/info
C++11 allows you to use braces to disambiguate initializations from declarations:
TimeHandling time{std::chrono::milliseconds(mpl)};
or, using braces for both initializations:
TimeHandling time{std::chrono::milliseconds{mpl}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With