cod*_*sus -5 c++ dictionary stl
我的代码在 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)
^
Run Code Online (Sandbox Code Playgroud)
我没有碰stl_tree.h,为什么会在那里发生错误?当我不知道从哪里开始时,我该如何调试这些类型的问题?
这是代码,我尝试以 name:points 的形式读取和插入数据到 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};
}
}
Run Code Online (Sandbox Code Playgroud)
通常,您从添加的最后一段代码开始。我猜是这个
piste_hakemisto = {nimi, pisteet};
Run Code Online (Sandbox Code Playgroud)
那应该是
piste_hakemisto.insert({nimi, pisteet});
Run Code Online (Sandbox Code Playgroud)
(做同样事情的其他方式也是可能的)。
这是模板的一个常见问题,错误消息通常是模糊的,并且引用了模板定义深处的代码。