"no matching function call..." 此错误来自哪里,我该如何解决?

"no matching function call..." Where is this error coming from and how can I resolve it?

本文关键字:何解决 解决 function matching no call 错误      更新时间:2023-10-16

我在第 3 行遇到了一个问题 - "调用 std::vector::p ush_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);
   }
}

您正在使用const_iterator进行迭代。根据定义,您无法修改const_iterator引用的内容。 请改用非常量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++更容易写和理解吗?