【问题标题】:"no matching function call..." Where is this error coming from and how can I resolve it?“没有匹配的函数调用......”这个错误来自哪里,我该如何解决?
【发布时间】:2016-06-06 10:16:00
【问题描述】:

我在第 3 行遇到了一个问题——“没有匹配的函数来调用 std::vector::push_back(int*)const”——有人可以向我解释这个问题来自哪里以及我该如何解决解决了吗?

for(int i = 1; i < 7; i++){
   for(vector< vector<int> >::const_iterator it = x.begin(); it < x.end(); it++){
      it->push_back(i);
   }
}

【问题讨论】:

  • 发布有关构建错误的问题时,请在问题正文中包含 实际 错误,使用复制粘贴显示完整(和未经编辑的)构建日志(包括任何信息注释和消息)。此外,如果可能,请尝试创建一个 Minimal, Complete, and Verifiable Example 来显示问题并显示该问题,或者至少显示所有相关变量的声明。您可能还想read about how to ask good questions

标签: c++ vector iterator


【解决方案1】:

您正在使用const_iterator 进行迭代。根据定义,您不能修改 const_iterator 引用的内容。请改用非 const iterator

for(vector< vector<int> >::iterator it = x.begin(); it != x.end(); it++){
      it->push_back(i);

或者,更好的是,您应该使用现代 C++11 或更高版本:

for (auto &x_vector: x)
     x_vector.push_back(i);

你不觉得现代 C++ 更容易编写和理解吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2016-09-09
    • 2021-11-23
    相关资源
    最近更新 更多