我正在编写一个教程,以在Windows 8.1的更新版本上使用Visual Studio Community 2017的最新版本学习C++。
当我调整以下.cpp文件并运行该程序时,输入错误后输入崩溃:
#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;
}
调试时,我进入xstring中提到的行,它给出以下错误:对于代码:
reference operator[](const size_type _Off)
{ // subscript mutable sequence
auto& _My_data = this->_Get_data();
_IDL_VERIFY(_Off <= _My_data._Mysize, "string subscript out of range");
return (_My_data._Myptr()[_Off]);
}
我执行的操作似乎触发了一个断点,但是代码反映了本教程中的代码,并且本教程的代码可以正确编译并运行。有人知道如何处理吗?我的搜索空无一物。
最佳答案
这看起来非常可疑:
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 []中,因此您应该一直在看这行,这是您发布的代码中唯一使用有问题的运算符的地方... :)
关于c++ - xstring在0x1005E5F6(ucrtbased.dll)抛出未处理的异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46081928/