[英]no matching function for call to std::vector<std::tuple> push_back
I have a sample program containing 6 timepoints using high_resolution_clock::now()
from standard chrono
header. 我有一个包含6个时间点的示例程序,使用来自标准
chrono
标头的high_resolution_clock::now()
。 I take differences b/w each of them resulting in 3 differences and caste them as auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
我对它们各自进行差异b / w导致3个差异,并将它们作为
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
to microseconds. 到微秒。
I have another variable named durations which is assigned as follows: auto durations = std::make_tuple(duration1,duration2,duration3);
我有另一个名为durations的变量,其分配如下:
auto durations = std::make_tuple(duration1,duration2,duration3);
containing previous time-point differences. 包含以前的时间点差异。
I have to push this tuple into an vector, so I have introduced std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
我必须把这个元组推到一个向量中,所以我引入了
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
However on using list.push_back(durations);
但是使用
list.push_back(durations);
I get an error as : 我得到一个错误:
prog.cpp: In function 'int main()':
prog.cpp:36:29: error: no matching function for call to 'std::vector<std::tuple<std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> > > >::push_back(std::tuple<long long int, long long int, long long int>&)'
list.push_back(durations);
I tried to search about std::chrono::microseconds
and other std::chrono::duration
stuff here but wasn't successful in rectifying the problem. 我试图寻找有关
std::chrono::microseconds
等std::chrono::duration
的东西在这里 ,但没有成功地纠正问题。
I know this has something to do with my negligence of type system, but I'm unable to locate that error. 我知道这与我对类型系统的疏忽有关,但我无法找到该错误。 Any help would be appreciated, & here is ideone link .
任何帮助将不胜感激,这里是ideone 链接 。
#include <iostream>
#include <chrono>
#include <vector>
#include <tuple>
using namespace std;
using namespace std::chrono;
void function()
{
long long number = 0;
for( long long i = 0; i != 2000000; ++i )
{
number += 5;
}
}
int main()
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
high_resolution_clock::time_point t3 = high_resolution_clock::now();
high_resolution_clock::time_point t5 = high_resolution_clock::now();
function();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
high_resolution_clock::time_point t4 = high_resolution_clock::now();
high_resolution_clock::time_point t6 = high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ).count();
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 ).count();
auto durations = std::make_tuple(duration1,duration2,duration3);
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
list.push_back(durations);
cout << duration1 << " -- "<< duration2 << " -- "<< duration3 << " -- ";
return 0;
}
You have created a tuple of 3 integers and you're trying to add it to a vector of 3 durations. 您已经创建了一个包含3个整数的元组,并且您尝试将其添加到3个持续时间的向量中。
I take differences b/w each of them resulting in 3 differences and caste them as
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
我对它们各自进行差异b / w导致3个差异,并将它们作为
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
to microseconds.到微秒。
Why are you calling count()
on the durations after doing the duration_cast
to convert to microseconds? 在执行
duration_cast
转换为微秒后,为什么要在持续时间内调用count()
?
Just keep the values as microseconds
objects and you can add them to the vector: 只需将值保存为
microseconds
对象,就可以将它们添加到向量中:
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 );
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 );
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 );
The type of std::chrono::microseconds::count()
is not std::chrono::microseconds
, it's some signed integral type. std::chrono::microseconds::count()
类型不是std::chrono::microseconds
,它是一些带符号的整数类型。
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
//decltype(duration1) is not std::chrono::microseconds
As such, you can't use your duration*n*
variables for a vector expecting microsecond
values. 因此,您不能将
duration*n*
变量用于期望microsecond
值的向量。
The fix is easy, just defer your call of count
until you try and print the contents. 修复很简单,只需推迟您的
count
调用,直到您尝试打印内容为止。
It's simple: don't call count
. 原因很简单:不调用
count
。
std::chrono::microseconds
is (in your case) a typedef for the type std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >
. std::chrono::microseconds
是(在你的情况下) std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >
的类型的typedef。 That is also the type you get from doing std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 )
. 这也是你从
std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 )
。
However, that is not what you're assigning to duration1
. 但是,这不是您分配给
duration1
。 You're assigning the result of calling the count
function on that type. 您正在为该类型分配调用
count
函数的结果。 And that returns the number of ticks as a number ( long long int
in your standard library's case), and not as duration
. 并且返回刻度数作为数字(标准库中的
long long int
),而不是duration
。
You are getting a type mismatch. 你得到的类型不匹配。
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count()
Actually gives you a long long
in your case and not a std::chrono::microseconds
. 实际上在你的情况下给你很
long long
而不是std::chrono::microseconds
。 You can fix this by using decltype()
and changing 你可以通过使用
decltype()
和更改来解决这个问题
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
To 至
std::vector<decltype(durations)> list;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.