函数访问冲突读取位置

function access violation reading location

本文关键字:位置 读取 访问冲突 函数      更新时间:2023-10-16

我有一个函数,当 return 语句在 else 内时,它只返回访问冲突读取位置错误,但当它在 else 之外时,没有错误被打开,但我有时会得到相同的返回值,当由于我不应该的代码。这是代码:

void askUser(){
    std::cout << "Level: ";
    std::cin >> level;
    for (int x = 0; x < 3; x++){
        word = getWord(level);
        std::cout << word << std::endl;
    }
}
std::string getWord(int level){
    int randomNumber = rand() % 3;
    if (usedWords[0].find(line[level - 1][randomNumber]) != usedWords[0].end()){
        getWord(level);
    }
    else{
        usedWords[0].insert(line[level - 1][randomNumber]);
        return line[level - 1][randomNumber];  //returns the access violation error
    }
    //if the return was here no access violation but can return previous return values which shouldn't happen
}

我不太确定究竟是什么导致了错误,但我认为这与 return 语句有关。

问题是当 if 语句中的条件为真时,您不会返回任何内容。 该函数说你应该返回一个字符串,但你只是到了函数的末尾。 未定义的行为。