如何在C++中打印high_resolution_clock
?high_resolution_clock錯誤:無法綁定'std :: ostream {aka std :: basic_ostream <char>}'lvalue to'std :: basic_ostream <char> &&'
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock high_resolution_clock;
int main()
{
std::cout << high_resolution_clock::now() << std::endl;
}
建築在上面的結果:
/home/greg/repositories/firstProject/main.cpp:27: error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1l, 1000000000l> > >}’)
std::cout << high_resolution_clock::now() << std::endl;
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
和
/home/greg/repositories/firstProject/main.cpp:27: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
std::cout << high_resolution_clock::now() << std::endl;
^
閱讀this answer後,我嘗試了 「容器」 迭代:
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock high_resolution_clock;
int main()
{
for(auto i: high_resolution_clock::now()){
std::cout << i << std::endl;
}
}
然而,這導致我n更多的錯誤。我也嘗試使用printf
和鑄造high_resolution_clock::now()
到long long
沒有成功。
更新:
嘗試另一種答案suggested here也取得了更多的錯誤:
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock high_resolution_clock;
int main()
{
auto i = high_resolution_clock::now();
std::cout << i.time_since_epoch() << std::endl;
}
[性病::計時和cout](HTTPS的可能的複製:// stackoverflow.com/questions/7889136/stdchrono-and-cout) – user1810087
@ user1810087我更新,這是在你鏈接的問題建議的代碼後。答案使用了不同的庫,該解決方案在本例中無效,因爲它導致了更多的編譯錯誤。 – GNettlefold