简体   繁体   English

没有用于调用std :: vector的匹配函数 <std::tuple> 推回

[英]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::microsecondsstd::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;

Live Example 实例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有用于调用 &#39;std::vector 的匹配函数<node*> ::push_back(节点*&amp;)&#39; - no matching function for call to ‘std::vector<node*>::push_back(Node*&)’ 没有匹配的函数调用&#39;std::vector &gt;::push_back(char&amp;)&#39; - no matching function for call to 'std::vector >::push_back(char&)' 没有匹配的 function 调用 'std::vector <std::vector<int> &gt;::push_back(int)' </std::vector<int> - no matching function for call to 'std::vector<std::vector<int> >::push_back(int)' 错误:没有匹配的 function 调用 'std::vector<float> ::push_back(std::vector<float> &amp;) 常量'</float></float> - error: no matching function for call to 'std::vector<float>::push_back(std::vector<float>&) const' 没有匹配的 function 调用 'std::vector::push_back(std::string&amp;)' - No matching function for call to ‘std::vector::push_back(std::string&)’ C++ - 在 foreach 中复制向量给出“没有匹配的函数来调用 std::vector<int> ::push_back(std::vector)<int> &amp;)” - C++ - copying vector in foreach gives "No matching function to call for std::vector<int>::push_back(std::vector<int>&)" 没有匹配的 function 调用 'std::vector::push_back(<brace-enclosed initializer list> )'</brace-enclosed> - no matching function for call to ‘std::vector::push_back(<brace-enclosed initializer list>)’ 错误:没有匹配的函数来调用“std::vector”<x> ::push_back(y&amp;) 在 C++ - error: no matching function for call to ‘std::vector<x>::push_back(y&) in C++ 填充向量的向量时出错:没有匹配的函数来调用std :: basic_string :: push_back - Error populating a vector of vectors: no matching function to call for std::basic_string::push_back 错误:没有匹配的函数调用&#39;std :: vector <std::vector<int> &gt; ::的push_back(标准::矢量 <std::__cxx11::basic_string<char> &gt;&)” - error: no matching function for call to ‘std::vector<std::vector<int>>::push_back(std::vector<std::__cxx11::basic_string<char> >&)’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM