12

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;
}
5
  • 2
    m is an int. You can't put an int into a vector of strings. Commented Jun 27, 2018 at 5:11
  • 3
    Upvoted because you included the entire error message. Commented Jun 27, 2018 at 5:15
  • Btw, include <string> Commented Jun 27, 2018 at 5:29
  • 1
    Ah, the woes of template error messages... Commented Jun 27, 2018 at 5:43
  • Particularilly bad because The error is supprisingly bigger than your code... Commented Feb 9, 2020 at 19:31

1 Answer 1

3

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>.

Sign up to request clarification or add additional context in comments.

5 Comments

Or convert the int to a std::string before pushing it
Or declare m as a std::string, which may have been the author's intent.
I have tried to convert a string to int then I got this error " terminate called after throwing an instance of 'std::out_of_range' what(): stoi Aborted (core dumped)"
@JeJo I Edited My Code as your advice but still going error here take a look on my code <code>#include <iostream> #include <vector> #include <algorithm> #include <string> int main(int argc, char const *argv[]) { std::vector<std::string> v; std::string m; int n; size_t s; std::cin >> n; for (size_t i = 0; i < n; i++) { std::cin >> m; v.push_back(std::stoi(m, &s)); } sort(v.begin(), v.end()); for(int i = 0; i < v.size(); i++){ std::cout << v[i] << '\n'; } return 0; } </code>
@Riajulkashem All you needed to do was change int m to string m. Only then can you do push_back(m)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.