1

I get a error like this :

conversion from ‘void’ to non-scalar type ‘std::vector<int>’ requested

and this a function I get this error from:

   vector<int> concat(vector<int> vector1, vector<int> vector2)
{
     return vector1.insert(vector1.end(), vector2.begin(), vector2.end());
}

I used the insert() function from algorithm library.

1
  • 1
    Hint: std::vector<int>::iterator isn't the same type as std::vector<int>.
    – user0042
    Oct 15, 2017 at 13:56

1 Answer 1

2

The method insert returns iterator.

It has the following declaration

template <class InputIterator>
iterator insert(const_iterator position, 
                InputIterator first, InputIterator last);

The function interface is not logically consistent.

Either declare the function the following way

std::vector<int> & concat( std::vector<int> &vector1, const std::vector<int> &vector2 )
{
    vector1.insert(vector1.end(), vector2.begin(), vector2.end());
    return vector1;
}

or the following way

std::vector<int> concat( const std::vector<int> &vector1, const std::vector<int> &vector2 )
{
    std::vector<int> result( vector1 );
    result.insert(result.end(), vector2.begin(), vector2.end());
    return result;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.