![](/img/trans.png)
[英]no matching function for call to ‘std::vector<node*>::push_back(Node*&)’
[英]no matching function for call to std::vector<std::tuple> push_back
我有一个包含6个时间点的示例程序,使用来自标准chrono
标头的high_resolution_clock::now()
。 我对它们各自进行差异b / w导致3个差异,并将它们作为auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
到微秒。
我有另一个名为durations的变量,其分配如下: 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);
我得到一个错误:
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);
我试图寻找有关std::chrono::microseconds
等std::chrono::duration
的东西在这里 ,但没有成功地纠正问题。
我知道这与我对类型系统的疏忽有关,但我无法找到该错误。 任何帮助将不胜感激,这里是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;
}
您已经创建了一个包含3个整数的元组,并且您尝试将其添加到3个持续时间的向量中。
我对它们各自进行差异b / w导致3个差异,并将它们作为
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
到微秒。
在执行duration_cast
转换为微秒后,为什么要在持续时间内调用count()
?
只需将值保存为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 );
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
因此,您不能将duration*n*
变量用于期望microsecond
值的向量。
修复很简单,只需推迟您的count
调用,直到您尝试打印内容为止。
原因很简单:不调用count
。
std::chrono::microseconds
是(在你的情况下) std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >
的类型的typedef。 这也是你从std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 )
。
但是,这不是您分配给duration1
。 您正在为该类型分配调用count
函数的结果。 并且返回刻度数作为数字(标准库中的long long int
),而不是duration
。
你得到的类型不匹配。
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count()
实际上在你的情况下给你很long long
而不是std::chrono::microseconds
。 你可以通过使用decltype()
和更改来解决这个问题
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
至
std::vector<decltype(durations)> list;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.