vendredi 22 décembre 2017

insert array to a vector not working

I have a uint8_t array, and I want to insert it to a vector vec, the code is sort of this:

uint8_t arr[] = { 1,2,3 };
std::vector<uint8_t> vec;
vec.insert(vec.begin(), arr, arr+3);
for (auto t : vec) {
    std::cout << unsigned(t) << " ";
}

this works fine in http://cpp.sh/, but when I move this into my real code, the compiler throw an error:

error: no matching member function for call to 'insert'
    buffer.insert(buffer.end(), hdr->send_data, hdr->send_data + hdr->data_len);
    ~~~~~~~^~~~~~
external/libcxx/include/vector:721:9: note: candidate function not viable: 'this' argument has type 'const std::vector<uint8_t>' (aka 'const vector<unsigned char>'), but method is not marked const
        insert(const_iterator __position, _ForwardIterator __first, _ForwardIterator __last);
        ^
external/libcxx/include/vector:700:14: note: candidate function not viable: 'this' argument has type 'const std::vector<uint8_t>' (aka 'const vector<unsigned char>'), but method is not marked const
    iterator insert(const_iterator __position, size_type __n, const_reference __x);
             ^
external/libcxx/include/vector:704:14: note: candidate template ignored: disabled by 'enable_if' [with _InputIterator = const unsigned char *]
             __is_input_iterator  <_InputIterator>::value &&
             ^
external/libcxx/include/vector:692:14: note: candidate function not viable: requires 2 arguments, but 3 were provided
    iterator insert(const_iterator __position, const_reference __x);
             ^
external/libcxx/include/vector:694:14: note: candidate function not viable: requires 2 arguments, but 3 were provided
    iterator insert(const_iterator __position, value_type&& __x);
             ^
external/libcxx/include/vector:724:14: note: candidate function not viable: requires 2 arguments, but 3 were provided
    iterator insert(const_iterator __position, initializer_list<value_type> __il)

This code is built in android AOSP, and the cflags is -std=c++11. I checked the header and it seems followed the c++ standard.

Aucun commentaire:

Enregistrer un commentaire