Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’

Tags:

c++

arrays

vector

I am new in c++. When I run my code got this error :(

Big Sorting.cpp: In function ‘int main(int, const char**)’: Big Sorting.cpp:13:22: error: no matching function for call to ‘std::vector >::push_back(int&)’ v.push_back(m); ^ In file included from /usr/include/c++/8.1.1/vector:64, from Big Sorting.cpp:2: /usr/include/c++/8.1.1/bits/stl_vector.h:1074:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::__cxx11::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::basic_string]’ push_back(const value_type& __x) ^~~~~~~~~ /usr/include/c++/8.1.1/bits/stl_vector.h:1074:7: note: no known conversion for argument 1 from ‘int’ to ‘const value_type&’ {aka ‘const std::__cxx11::basic_string&’} /usr/include/c++/8.1.1/bits/stl_vector.h:1090:7: note: candidate: ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::__cxx11::basic_string; _Alloc = std::allocator >; std::vector<_Tp, _Alloc>::value_type = std::__cxx11::basic_string]’ push_back(value_type&& __x) ^~~~~~~~~ /usr/include/c++/8.1.1/bits/stl_vector.h:1090:7: note: no known conversion for argument 1 from ‘int’ to ‘std::vector >::value_type&&’ {aka ‘std::__cxx11::basic_string&&’}

here is my code

#include <iostream>
#include <vector>
#include <algorithm>

int main(int argc, char const *argv[]) {
    std::vector<std::string> v;

    int n, m;
    std::cin >> n;
    for (size_t i = 0; i < n; i++) {
        std::cin >> m;
        v.push_back(m);
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); i++){
        std::cout << v[i] << '\n';
    }
    return 0;
}
like image 409
Riajul kashem Avatar asked Jun 27 '18 05:06

Riajul kashem


People also ask

How to fix the error no matching function for call to in c++?

We mismatch the parameters to the function. We might be required to give the matched parameter to the specified method. Or we have to add a new function with the same data type. After checking and adding suitable parameters to the function in the program, the error, 'no matching function for a call' will be resolved.

What does std :: vector mean?

1) std::vector is a sequence container that encapsulates dynamic size arrays. 2) std::pmr::vector is an alias template that uses a polymorphic allocator. The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements.


1 Answers

You are reading int variable m and trying to put it into a vector of strings. You should use std::vector<int> instead.

Bottom line: your code needs only one change, most reasonable one would be to change std::vector<std::string> to std::vector<int>.

like image 88
r3mus n0x Avatar answered Sep 17 '22 00:09

r3mus n0x