我正在使用最新版本的Visual Studio Community 2017在Windows 8.1的更新版本上学习C ++的教程。
当我调整以下.cpp文件并运行程序时,在输入错误输入后它崩溃了:
Debug Assertion失败!
程序C:\ WINDOWS \ SYSTEM32 \ MSVCP140D.dll文件c:\ program files(x86)\ microsoft visual studio \ 2017 \ community \ vc \ tools \ msvc \ 14.11.25503 \ include \ xstring 行:2969
表达式字符串下标超出范围
XmlSerializer
调试时,它将我带到xstring中引用的行,它会出现以下错误:
BullCowGame.exe中0x1005E5F6(ucrtbased.dll)的未处理异常:将无效参数传递给认为无效参数致命的函数。
代码:
#include "stdafx.h"
#include "FBullCowGame.h"
using int32 = int;
FBullCowGame::FBullCowGame() { Reset(); }
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
void FBullCowGame::Reset()
{
constexpr int32 MAX_TRIES = 8;
MyMaxTries = MAX_TRIES;
const FString HIDDEN_WORD = "ant";
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
return;
}
bool FBullCowGame::IsGameWon () const { return false; }
bool FBullCowGame::CheckGuessValidity(FString)
{
return false;
}
// Receives a VALID guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitGuess(FString Guess)
{
// incriment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 i = 0; i < Guess.length(); i++) {
// compare letters against the hidden word
for (int32 j = 0; j < HiddenWordLength; j++) {
// if they match then
if (Guess[j] == MyHiddenWord[i]) {
if (i == j) { // if they're in the same place
BullCowCount.Bulls++; // incriment bulls
}
else {
BullCowCount.Cows++; // must be a cow
}
}
}
}
return BullCowCount;
}
似乎有一些断点由我所做的事情触发,但代码反映了教程中的代码,教程代码编译并正确运行。
有谁知道如何处理这个问题?我的搜索空白了。
答案 0 :(得分:0)
这看起来非常可疑:
for (int32 i = 0; i < Guess.length(); i++) {
// compare letters against the hidden word
for (int32 j = 0; j < HiddenWordLength; j++) {
...
注意, i 以猜测的长度为界, j 以HiddenWord的长度为界。
请注意,您在这里混淆了索引变量:
// if they match then
if (Guess[j] == MyHiddenWord[i]) {
此外,您应该一直在查看此行,因为异常位于operator []中,并且这是您发布的代码中唯一使用相关运算符的地方......:)