C ++-在foreach中复制vector给出了“没有匹配函数来调用std :: vector <int> :: push_back(std :: vector <int>&)”

时间:2019-12-20 15:09:28

标签: c++ c++11 stdvector

我具有以下功能:

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;
}

当我编写以下代码以获得结果时:

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);            
        }
    }
}

我收到以下错误:

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);

据我了解,该范围所基于的是创建col作为参考。我不了解的是push_back能够插入col。为什么会发生这种情况?将col插入results[t]的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

colvector<int>

您正在尝试将其添加到results的元素中,该元素只能容纳int

这就是编译器告诉你的。