【发布时间】:2016-10-07 18:06:47
【问题描述】:
我正在使用一个库来解析 .pgn 文件,当我尝试运行该项目时,我发现了这个错误:Debug Assertion Failed!程序:C:\windows\SYSTEM32\MSVCP110D.dll 文件:c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstring 线路:79 表达式:字符串迭代器不可取消引用 有关您的程序如何导致断言失败的信息,请参阅关于断言的可视 C++ 文档。
问题是当迭代器到达文件末尾时,它没有指向任何东西(start iterator (itr1) == end iterator (itr2)),我尝试添加条件来检查itr1是否到达末尾的文件,但它是无用的。 所以请告诉我我的错在哪里。 这是我的代码 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;
}
这是导致错误的类函数:
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 函数:
void pgn::Parser::skipBlanks(std::string::const_iterator &itr1, cost std::string::const_iterator &end)
{
while((itr1 != end) && (isspace(*itr1)))
{
itr1++;
}
}
我已经在 stackoverflow 和 google 搜索了所有类似的问题,但我找不到答案。我也逐行跟踪代码,直到找到导致错误的函数。
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
在这里提问之前我已经逐行调试了代码,但是找不到答案!
-
那你可能用错了调试器。
-
使用调试器帮助我找到了导致错误的函数,但我无法修复它。我正在使用一个不是我编写的库,我尝试对其进行编辑以解决错误,但我不能。