我在std::chrono上遇到了一个奇怪的错误,
如果我做这样的事情:
TimeHandling time(std::chrono::milliseconds(1000 / 125));
time.start();
一切都好。
但是,如果我将毫秒值放在变量中:
int mpl = 1000 / 125;
TimeHandling time(std::chrono::milliseconds(mpl));
time.start();
g++抛出此错误:
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> >)}’
有人知道为什么吗?
最佳答案
第二个版本使用名为mpl
的参数声明一个函数
参见http://en.wikipedia.org/wiki/Most_vexing_parse和https://stackoverflow.com/tags/most-vexing-parse/info
C++ 11允许您使用花括号来消除声明中的初始化:
TimeHandling time{std::chrono::milliseconds(mpl)};
或者,对两个初始化都使用大括号:
TimeHandling time{std::chrono::milliseconds{mpl}};