未处理的异常,传递给认为无效参数致命的函数的无效参数
unhandled exception, invalid parameter passed to a function that considers invalid paramaters fatal
我正在使用 visual studio 2017,并根据 unreal engine 编码标准进行编码,它抛出了一个未处理的异常,其中传递给认为它们是致命参数的函数的无效参数。我想不通,VS2017 调试器完全没用,而且我对编码很陌生,有人可以给我一些建议吗?编辑:我唯一能接近发现的是,它似乎是由以下函数中某处的无限循环导致的字符串超出范围引起的。
FBullCowCount FBullCowGame::SubmitGuess(FText Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
//if they match then
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
{
//increment bulls if they're in the same place
if (MHWChar == GChar) {
BullCowCount.Bulls++;
}
else {
BullCowCount.Cows++;
}
}
} //increment cows if not
}
return BullCowCount;
}
你的代码注释说 "loop through all letters in the guess",但你的代码循环遍历 MyHiddenWord
的所有字母。这意味着除非 Guess
和 MyHiddenWord
的长度完全相同,否则:
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
将在某个时候访问超出范围的 Guess
元素,如果 FText
恰好使用范围检查 operator[]
.
你可能想要的是:
#include <algorithm>
// ...
auto HiddenWordLength = std::min(MyHiddenWord.length(), Guess.length());
它将限制循环遍历两个字符串中较短字符串的字母数量。
我正在使用 visual studio 2017,并根据 unreal engine 编码标准进行编码,它抛出了一个未处理的异常,其中传递给认为它们是致命参数的函数的无效参数。我想不通,VS2017 调试器完全没用,而且我对编码很陌生,有人可以给我一些建议吗?编辑:我唯一能接近发现的是,它似乎是由以下函数中某处的无限循环导致的字符串超出范围引起的。
FBullCowCount FBullCowGame::SubmitGuess(FText Guess)
{
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
// loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) {
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
//if they match then
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
{
//increment bulls if they're in the same place
if (MHWChar == GChar) {
BullCowCount.Bulls++;
}
else {
BullCowCount.Cows++;
}
}
} //increment cows if not
}
return BullCowCount;
}
你的代码注释说 "loop through all letters in the guess",但你的代码循环遍历 MyHiddenWord
的所有字母。这意味着除非 Guess
和 MyHiddenWord
的长度完全相同,否则:
if (Guess[MHWChar] == MyHiddenWord[MHWChar])
将在某个时候访问超出范围的 Guess
元素,如果 FText
恰好使用范围检查 operator[]
.
你可能想要的是:
#include <algorithm>
// ...
auto HiddenWordLength = std::min(MyHiddenWord.length(), Guess.length());
它将限制循环遍历两个字符串中较短字符串的字母数量。