Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:请求从std::chrono::time_点浮点转换为非标量类型long int_C++_C++11_Chrono - Fatal编程技术网

C++ 错误:请求从std::chrono::time_点浮点转换为非标量类型long int

C++ 错误:请求从std::chrono::time_点浮点转换为非标量类型long int,c++,c++11,chrono,C++,C++11,Chrono,我有一段代码试图通过添加持续时间来创建新的时间点: // now std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); // duration std::chrono::duration<float> duration(1.0f / 60.0f); // float in seconds // now + duration std::chrono::system_clock::tim

我有一段代码试图通过添加持续时间来创建新的时间点:

// now
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// duration
std::chrono::duration<float> duration(1.0f / 60.0f); // float in seconds
// now + duration
std::chrono::system_clock::time_point futureTime = now + duration;
//现在
std::chrono::system_clock::time_point now=std::chrono::system_clock::now();
//持续时间
标准::计时::持续时间(1.0f/60.0f);//秒内浮动
//现在+持续时间
标准::时钟::系统时钟::时间点未来时间=现在+持续时间;
但它给了我这个错误

error: conversion from 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<float, std::ratio<1, 1000000000> > >' to non-scalar type 'std::chrono::_V2::system_clock::time_point {aka std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >}' requested
# it's over there in the distance --->

# ... again, with some manual formatting:
error:
    conversion from
    'std::chrono::time_point<std::chrono::_V2::system_clock,
                             std::chrono::duration<float, 
                                                   std::ratio<1, 1000000000> > >'
    to non-scalar type
    'std::chrono::_V2::system_clock::time_point
     {aka 
     std::chrono::time_point<std::chrono::_V2::system_clock,
                             std::chrono::duration<long int,
                                                   std::ratio<1, 1000000000> > >}'
    requested
错误:请求将“std::chrono::time_point”转换为非标量类型“std::chrono::_V2::system_clock::time_point{aka std::chrono::time_point}”
#就在那边远处--->
# ... 同样,使用一些手动格式:
错误:
从
'std::chrono::time\u point'
到非标量类型
'std::chrono::_V2::系统时钟::时间点
{aka
std::chrono::time_point}'
请求
在一些严重的斜视之后,第一个有一个
float
,第二个有一个
long int
。因此,
now(long int)+duration(float)
给出了一个带有一些内部浮点duration的时间点,我大概要求将它填充回默认的
long int
表示形式中

我希望最终将这个时间点传递到
std::condition\u variable::wait\u,直到
。如何强制转换到
std::chrono::system\u clock::time\u point

如果我这样做,我会损失什么样的精度(即它是否存储毫秒,在这种情况下,我会损失一些1/60)


如果我不转换它,那么写
now+duration
返回的类型(是的,我可以使用auto,但为了把它传递给函数)的一个很好的短方法是什么呢?

std::condition\u variable::wait\u,直到
如下所示:()

template
标准::cv_状态
等待直到(标准::唯一锁定和锁定,
const std::chrono::time\u point&timeout\u time);
模板<班级时钟、班级持续时间、班级预测>
bool等待直到(标准::唯一锁定和锁定,
常数标准::计时::时间点和超时时间,
Pred(Pred);
您可以将任何
时间点
传递给它。它不必是
系统时钟::时间点
。而
系统时钟::时间点
就是
时间点

时钟的精确表示由实现定义,但必须是整数类型。不能用整数作为数字,秒作为单位来精确表示
(1/60)s


now+duration
,其中
now
类型为
time\u point
duration
类型为
duration
,类型为
time\u point

,您在问题中所做的分析基本正确。有两种很好的方法可以让它发挥作用

使用
auto
time\u point\u cast
是您如何投射
time\u point
s。您必须明确指定所产生的
时间点
的持续时间类型。生成的
时间点将继续基于相同的时钟。
结果将被截断(四舍五入到零)到下一个整数
系统时钟::持续时间
此平台上的纳秒

此类型可与
std::condition\u variable::wait\u until
一起使用

现在整轮<代码>加持续时间
返回<代码>系统时钟::时间点
: 人们常说:

不可能精确地表示1/60秒

但是您可以使用
:-)<代码>持续时间
使用
int
表示以1/60秒为单位计数<代码>持续时间{1}
为1/60秒<代码>持续时间{2}
为2/60秒。等等


auto
在这里真的很方便。
futureTime
的类型是:
time\u point“正在存储毫秒”它存储您要求它存储的内容。另外,如果您想等待1/60秒,有什么原因不能直接使用
wait\u for
,因为这正是它的目的?旁注:在Windows中,让事情在1/60秒内发生可能会很烦人,因为Windows使用1/64的刻度。@Nicolas
std::chrono::system\u clock::time\u point
似乎隐式地使用了纳秒和
std::ratio
。我没有要求它这么做。我想等到1/60秒过去,使用
wait_-until
我可以一直调用它,直到它在我被中断时超时,而不是等待。
template< class Clock, class Duration >
std::cv_status
    wait_until( std::unique_lock<std::mutex>& lock,
                const std::chrono::time_point<Clock, Duration>& timeout_time );

template< class Clock, class Duration, class Pred >    
bool wait_until( std::unique_lock<std::mutex>& lock,
                 const std::chrono::time_point<Clock, Duration>& timeout_time,
                 Pred pred );
// now
system_clock::time_point now = system_clock::now();
// duration
duration<float> duration(1.0f / 60.0f); // float in seconds
// now + duration
auto futureTime = now + duration;
// now
system_clock::time_point now = system_clock::now();
// duration
duration<float> duration(1.0f / 60.0f); // float in seconds
// now + duration
system_clock::time_point futureTime =
    time_point_cast<system_clock::duration>(now + duration);
system_clock::time_point futureTime = round<system_clock::duration>(now + duration);
// now
system_clock::time_point now = system_clock::now();
// duration
duration<int, std::ratio<1, 60>> duration{1}; // 1/60 of a second
// now + duration
auto futureTime = now + duration;
using namespace std::chrono;