Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vector push_back returns invalid conversion

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])’

>Solution :

The issue is that you are using a vector<int> rather than a vector<string>

#include <vector>
#include <iostream>
using namespace std;
int main() {
    vector<string> 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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading