简体   繁体   English

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>&)"

I have the following function:我有以下功能:

std::vector<std::vector<int>> solve(int t){
    std::vector<std::vector<int>> result;
    result.push_back(std::vector<int>(2*t,0));
    //CODE TO fill up result[0]
    return result;
}

And when I write the following code to get results:当我编写以下代码以获得结果时:

std::vector<std::vector<int>> results(4);
for(int t = 0; t < 4; ++t){
    std::vector<std::vector<int>> cols = solve(t);
    if(cols.size() > 0){
        for(std::vector<int> col: cols){
            results[t].push_back(col);            
        }
    }
}

I get the following error:我收到以下错误:

src/pricing.cpp:33:29: error: no matching function for call to ‘std::vector<int>::push_back(std::vector<int>&)’
 results[t].push_back(col);

From what I understand the range based for is creating col as a reference.据我了解,基于的范围是创建col作为参考。 What I don't understand is push_back being able to insert col .我不明白的是push_back能够插入col Why is this happening and what's the best way to insert col into results[t] ?为什么会发生这种情况,将col插入results[t]的最佳方法是什么?

col is a vector<int> . col是一个vector<int>

You're attempting to add that to an element of results , which can only hold int s.您正在尝试将其添加到results元素中,该元素只能保存int s。

That's what the compiler is telling you.这就是编译器告诉你的。

暂无
暂无

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

相关问题 没有匹配的 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)' C ++简单的树遍历并将行存储在vector的行中。 错误:没有匹配的函数可以调用&#39;std :: vector <int> :: push_back(std :: vector <int> &)&#39; - C++ Simple tree traversal and storing rows in vector's rows. Error: no matching function for call to 'std::vector<int>::push_back(std::vector<int>&)' 错误:没有匹配的函数调用&#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> >&)’ 没有用于调用std :: vector的匹配函数 <std::tuple> 推回 - no matching function for call to std::vector<std::tuple> push_back 错误:没有匹配函数来调用&#39;std :: vector <std::__cxx11::basic_string<char> &gt; ::的push_back(INT&)” - error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’ 错误:没有匹配的函数来调用“std::vector”<x> ::push_back(y&amp;) 在 C++ - error: no matching function for call to ‘std::vector<x>::push_back(y&) in C++ 的std ::矢量 <std::vector<int> &gt; push_back产生堆缓冲区溢出 - std::vector<std::vector<int>> push_back gives heap-buffer-overflow C ++如何将数组int [10] push_back到std :: vector <int[10]> ? - C++ how to push_back an array int[10] to std::vector<int[10]>? 没有用于调用 &#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&)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM