我正试图在 mingw64 (GCC v11.2) 中构建我的程序。我有以下的 struct:
在一个头文件中:
struct Timer
{
std::chrono::time_point< std::chrono::steady_clock > start;
std::chrono::time_point< std::chrono::steady_clock > end;
Timer( );
~Timer( );
};
在源代码文件中:
util::Timer::Timer( )
: start( std::chrono::high_resolution_clock::now( ) )
{
}
util::Timer::~Timer( )
{
end = std::chrono::high_resolution_clock::now( );
std::chrono::duration< double, std::milli > duration_ms { end - start };
std::clog << "\nTimer took " << duration_ms.count( ) << " ms\n";
}
但是发生了这种情况:
error: no matching function for call to 'std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long long int, std::ratio<1, 1000000000> > >::time_point(std::chrono::_V2::system_clock::time_point)'
8 | : start( std::chrono::high_resolution_clock::now( ) )
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from pch.h:23:
c:\mingw64\include\c++\11.2.0\chrono:871:21: note: candidate: 'template<class _Dur2, class> constexpr std::chrono::time_point<_Clock, _Dur>::time_point(const std::chrono::time_point<_Clock, _Dur2>&) [with _Dur2 = _Dur2; <template-parameter-2-2> = <template-parameter-1-2>; _Clock = std::chrono::_V2::steady_clock; _Dur = std::chrono::duration<long long int, std::ratio<1, 1000000000> >]'
871 | constexpr time_point(const time_point<clock, _Dur2>& __t)
| ^~~~~~~~~~
c:\mingw64\include\c++\11.2.0\chrono:871:21: note: template argument deduction/substitution failed:
Util.cpp:8:3: note: mismatched types 'std::chrono::_V2::steady_clock' and 'std::chrono::_V2::system_clock'
8 | : start( std::chrono::high_resolution_clock::now( ) )
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
为什么会出现这种情况?如何解决?
std::chrono::steady_clock
和std::chrono::high_resolution_clock
并不相同(必须区分)。不能混用它们。 - Paul Sandershigh_resolution_clock
是steady_clock
或者system_clock
的别名。具体使用哪个取决于实现,而且甚至会因版本不同而改变。 - NathanOliverstart
和end
改为std::chrono::time_point<std::chrono::high_resolution_clock>
?或者使用std::chrono::steady_clock::now()
?基本上,不要混合它们。 - Kevin