简体   繁体   中英

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. What I don't understand is push_back being able to insert col . Why is this happening and what's the best way to insert col into results[t] ?

col is a vector<int> .

You're attempting to add that to an element of results , which can only hold int s.

That's what the compiler is telling you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM