C++ 错误:没有用于调用“std::vector<std::__cxx11::basic_string<char> >::push_back(int&)”的匹配函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51054902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error: no matching function for call to ‘std::vector<std::__cxx11::basic_string<char> >::push_back(int&)’
提问by Riajul kashem
I am new in c++. When I run my code got this error :(
我是 C++ 新手。当我运行我的代码时出现此错误:(
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&&'}
Big Sorting.cpp:在函数“int main(int, const char**)”中:Big Sorting.cpp:13:22:错误:没有匹配的函数调用“std::vector >::push_back(int&)” v.push_back(m); ^ 在来自 /usr/include/c++/8.1.1/vector:64 的文件中,来自 Big Sorting.cpp:2:/usr/include/c++/8.1.1/bits/stl_vector.h:1074:7:注意:候选:'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:注意:参数 1 从“int”到“const value_type&”没有已知的转换 {aka 'const std::__cxx11::basic_string&'} /usr/include/c++/8.1.1/bits/stl_vector.h:1090:7:注意:候选:'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: 注意:参数 1 从 'int' 到 'std::vector >::value_type&&' {aka 'std::__cxx11::basic_string&&'} 没有已知的转换 _Alloc>::value_type&&) [与 _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: 注意:参数 1 从 'int' 到 'std::vector >::value_type&&' {aka 'std::__cxx11::basic_string&&'} 没有已知的转换 _Alloc>::value_type&&) [与 _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:注意:参数 1 从 'int' 到 '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;
}
回答by r3mus n0x
You are reading int
variable m
and trying to put it into a vector of strings. You should use std::vector<int>
instead.
您正在读取int
变量m
并尝试将其放入字符串向量中。你应该std::vector<int>
改用。
Bottom line: your code needs only one change, most reasonable one would be to change std::vector<std::string>
to std::vector<int>
.
底线:您的代码只需要进行一项更改,最合理的更改是更改std::vector<std::string>
为std::vector<int>
.