[英]string iterator is not dereferencable
I am using a library to parse .pgn file, and when I tried to run the project I found this error:Debug Assertion Failed! 我正在使用一个库来解析.pgn文件,当我尝试运行该项目时,我发现了以下错误:调试断言失败! program: C:\\windows\\SYSTEM32\\MSVCP110D.dll File: c:\\program files (x86)\\microsoft visual studio 11.0\\vc\\include\\xstring Line:79 Expression:string iterator not dereferencable For information on how your program can cause an assertion failure, see the visual C++ documentation on asserts.
程序:C:\\ windows \\ SYSTEM32 \\ MSVCP110D.dll文件:c:\\ program files(x86)\\ microsoft visual studio 11.0 \\ vc \\ include \\ xstring Line:79 Expression:字符串迭代器不可取消有关程序如何引起的信息断言失败,请参见有关断言的可视C ++文档。
the problem is that when the iterator reaches to the end of the file, it points to nothing (start iterator (itr1) == end iterator (itr2)), I tried to add conditions to check if itr1 reached to the end of the file but it was useless. 问题是,当迭代器到达文件末尾时,它什么都没有指向(开始迭代器(itr1)==结束迭代器(itr2)),我试图添加条件以检查itr1是否到达文件末尾但这没用。 So please tell me where is my fault.
所以,请告诉我我的错在哪里。 here is my code source.cpp file:
这是我的代码source.cpp文件:
#include <iostream>
#include <fstream>
#include <PGNGameCollection.h>
int main()
{
std::ifstream pgnfile("sample.pgn");
pgn::GameCollection games;
pgnfile >> games;
std::cout << "the file sample.pgn contains " << games.size() << "games" << std::endl;
system("pause");
return 0;
}
and here is the class function that causes the error: 这是导致错误的类函数:
bool pgn::Parser::getComment(std::string::const_iterator &itr1, const std::string::const_iterator &itr2, pgn::CommentText &out)
{
std::string::const_iterator local_itr=itr1;
std::string comment;
if(*local_itr != '{')
return false;
local_itr++; //skipping '{'
while((*local_itr != '}') && (local_itr != itr2))
{
comment += *local_itr++;
}
local_itr++; //skipping '}'
skipBlanks(local_itr, itr2);
itr1=local_itr;
out=pgn::CommentText(comment);
return true;
}
skipBlanks function: skipBlanks函数:
void pgn::Parser::skipBlanks(std::string::const_iterator &itr1, cost std::string::const_iterator &end)
{
while((itr1 != end) && (isspace(*itr1)))
{
itr1++;
}
}
I have searched stackoverflow and google for all similar problems but I could not find the answer. 我已经在stackoverflow和google中搜索了所有类似的问题,但是找不到答案。 also I traced the code line by line until I have reached to the function that caused the error.
我也逐行跟踪代码,直到找到导致错误的函数为止。
If itr2
is your end-iterator, then you must check your iterator for the end-condition before trying to dereference it 如果
itr2
是您的最终迭代器,那么在尝试取消引用之前 ,您必须检查迭代器的最终条件
while((local_itr != itr2) && (*local_itr != '}'))
You are doing it the other way around, which would definitely lead to the problem you described. 您正在以相反的方式进行操作,这肯定会导致您描述的问题。
It might also make sense to add checks for end-condition at the very beginning of your function, since there you are also dereferencing local_itr
. 在函数的最开始添加对结束条件的检查也可能是有意义的,因为在那里您还取消了对
local_itr
引用。
Also, if your cycle terminated because local_itr
reached itr2
, and there's nothing at itr2
and after it, then the code after the cycle makes no sense. 另外,如果您的循环由于
local_itr
到达了itr2
而终止,并且在itr2
及其之后没有任何内容,那么循环之后的代码将变得毫无意义。 You are not allowed to increment local_itr
in that situation. 在这种情况下,不允许您增加
local_itr
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.