【问题标题】:No match for operator++ in stl_tree.h even though i haven't changed itstl_tree.h 中的 operator++ 不匹配,即使我没有更改它
【发布时间】:2018-02-02 18:50:14
【问题描述】:

我的代码在 main.cpp 中正常工作,但突然间,我得到了这个错误:

/opt/lintula/gcc/include/c++/5.2.0/bits/stl_tree.h:2223: error: no match for ‘operator++’ (operand type is ‘std::__cxx11::basic_string<char>’)
  for (; __first != __last; ++__first)
                            ^

我没有碰过stl_tree.h,为什么会出现错误?当我不知道从哪里开始时,我该如何调试这些类型的问题?

这是代码,我尝试以以下形式读取和插入数据 名称:指向 std::map

#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <fstream>

using namespace std;

int main()
{
   map<string, string> piste_hakemisto;
   string input_tiedosto;
   cout << "Input file: ";
   cin >> input_tiedosto;
   ifstream tiedosto_olio(input_tiedosto);
   if (not tiedosto_olio){
       cout << "Error! The file " << input_tiedosto << " cannot be opened.";
       return EXIT_FAILURE;
   }
   string nimi;
   string pisteet;
   string rivi;
   while( getline(tiedosto_olio, rivi)){
       getline(tiedosto_olio, rivi);
       int erotin_indeksi = rivi.find(":");
       nimi = rivi.substr(0, erotin_indeksi);
       pisteet = rivi.substr((erotin_indeksi));
       piste_hakemisto = {nimi, pisteet};
   }

}

【问题讨论】:

  • 这很可能不是您遇到的唯一错误。也许是第一个,但在错误流中的某个地方是对您的文件的引用。
  • 是的,这不是唯一的错误,但我找不到对 main.cpp 的任何引用,function.h 和 stl_iterator_base_types.h 中存在错误
  • 你的意思是写piste_hakemisto[nimi] = pisteet;而不是piste_hakemisto = {nimi, pisteet};
  • john 给了我一个很好的解决方案。我之前收到了来自 QT 的模糊错误消息,不知道为什么,但现在它似乎可以与 map::insert 一起使用。

标签: c++ dictionary stl


【解决方案1】:

通常从您添加的最后一段代码开始。我猜是这个

piste_hakemisto = {nimi, pisteet};

应该是这样的

piste_hakemisto.insert({nimi, pisteet});

(做同样事情的其他方式也是可能的)。

这是模板的常见问题,错误消息通常是模糊的,并且引用了模板定义中的代码。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    相关资源
    最近更新 更多