【问题标题】:Vector push_back returns invalid conversion [closed]矢量 push_back 返回无效转换 [关闭]
【发布时间】:2022-01-26 15:07:24
【问题描述】:

我是 cpp 向量的新手,我一直遇到这个问题

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;
}

这是错误

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

【问题讨论】:

  • 这个错误说明了一切:test.cpp:6:19: error: invalid conversion from ‘const char*’ to ‘std::vector&lt;int&gt;::value_type’ {aka ‘int’} [-fpermissive]

标签: c++ c++11 vector


【解决方案1】:

问题是您使用的是vector&lt;int&gt; 而不是vector&lt;string&gt;

#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;
}

【讨论】:

  • 值得指出的是,我们如何通过编译器打印出的错误语句找到错误的根源
猜你喜欢
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多