lundi 27 décembre 2021

Vector push_back returns invalid conversion [closed]

Im new to cpp vectors and i keep encountering this issue

error: no matching function for call to ‘push_back(const char [6])’

#include <vector>
#include <iostream>
using namespace std;
int main() {
    vector<int> names;
    names.push_back("Lewis");
    names.push_back("Mark");
    names.push_back("Reece");
    names.push_back("Max");
    for(int i = 0; i < names.size(); i++)
        cout << names[i] << '\n';
    return 0;
}

This is the error

test.cpp: In function ‘int main()’:
test.cpp:6:26: error: no matching function for call to ‘push_back(const char [6])’
    6 |   names.push_back("Lewis");
      |                          ^
In file included from /usr/include/c++/9/vector:67,
                 from test.cpp:1:
/usr/include/c++/9/bits/stl_vector.h:1184:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]’ <near match>
 1184 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/include/c++/9/bits/stl_vector.h:1184:7: note:   conversion of argument 1 would be ill-formed:
test.cpp:6:19: error: invalid conversion from ‘const char*’ to ‘std::vector<int>::value_type’ {aka ‘int’} [-fpermissive]
    6 |   names.push_back("Lewis");
      |                   ^~~~~~~
      |                   |
      |                   const char*
In file included from /usr/include/c++/9/vector:67,
                 from test.cpp:1:
/usr/include/c++/9/bits/stl_vector.h:1200:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::value_type = int]’ <near match>
 1200 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/include/c++/9/bits/stl_vector.h:1200:7: note:   conversion of argument 1 would be ill-formed:
test.cpp:6:19: error: invalid conversion from ‘const char*’ to ‘std::vector<int>::value_type’ {aka ‘int’} [-fpermissive]
    6 |   names.push_back("Lewis");
      |                   ^~~~~~~
      |                   |
      |                   const char*
test.cpp:7:25: error: no matching function for call to ‘push_back(const char [5])’

Aucun commentaire:

Enregistrer un commentaire