我不明白如何在 Visual Studio 中修复此功能

发布于 2025-01-20 19:42:28 字数 8 浏览 0 评论 0原文

continue

I'm getting the following error in Visual Studio that I don't understand:

main.cpp(21): error C2440: '=': cannot convert from 'void' to 'char'
main.cpp(21): note: Expressions of type void cannot be converted to other types

This is my code:

void getLetterGrade(double totalAvg, char& letter_Grade)
{
    if (totalAvg >= 90) {
        letter_Grade = 'A';
    }
    else if (totalAvg < 90 && totalAvg >= 80) {
        letter_Grade = 'B';
    }
    else if (totalAvg < 80 && totalAvg >= 70) {
        letter_Grade = 'C';
    }
    else if (totalAvg < 70 && totalAvg >= 60) {
        letter_Grade = 'F';
    }
}

int main()
{
    double totalGrade = 74;
    char letterGRADE;
    letterGRADE = getLetterGrade(totalGrade, letterGRADE);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2025-01-27 19:42:28

您的函数 getLetterGrade 返回 void,这意味着“什么都没有”。因此,在赋值的右侧使用它是非法的。

这里

letterGRADE = getLetterGrade(totalGrade, letterGRADE);

会导致错误,因为它尝试使用函数的返回值,但没有。

但是,由于您通过引用传递第二个参数(通过说“char & letter_Grade”),您已经获得了所需的字母。只需将您的代码更改为

getLetterGrade(totalGrade, letterGRADE);

即可。当被调用函数返回时,变量letterGRADE的值已经被更新。

作为替代方案,您可以更改函数以返回字母:

char getLetterGrade(double totalAvg)
{
    char letter_Grade = '?';
    if (totalAvg >= 90)
    {
        letter_Grade = 'A';
    }
    else if (totalAvg < 90 && totalAvg >= 80)
    {
        letter_Grade = 'B';
    }
    else if (totalAvg < 80 && totalAvg >= 70)
    {
        letter_Grade = 'C';
    }
    else if (totalAvg < 70)
    {
        // Anything below 70 is flunked, so no need to give an upper limit (otherwise an average of 50 would not get a mark at all)
        letter_Grade = 'F';
    }
    return letter_Grade;
}

现在您可以像这样调用它,使用返回值:

letterGRADE = getLetterGrade(totalGrade);

Your function getLetterGrade returns void, that means "nothing". Therefore it is illegal to use it on the right hand side of an assignment.

This here

letterGRADE = getLetterGrade(totalGrade, letterGRADE);

will result in an error, because it tries to use the return value of the function, but there is none.

But, since you pass the second parameter by reference (by saying "char & letter_Grade") you already get the required letter back. Just change your code to

getLetterGrade(totalGrade, letterGRADE);

and you should be fine. The value of the variable letterGRADE will already have been updated when the called function returns.

As an alternative, you could change the function to return the letter instead:

char getLetterGrade(double totalAvg)
{
    char letter_Grade = '?';
    if (totalAvg >= 90)
    {
        letter_Grade = 'A';
    }
    else if (totalAvg < 90 && totalAvg >= 80)
    {
        letter_Grade = 'B';
    }
    else if (totalAvg < 80 && totalAvg >= 70)
    {
        letter_Grade = 'C';
    }
    else if (totalAvg < 70)
    {
        // Anything below 70 is flunked, so no need to give an upper limit (otherwise an average of 50 would not get a mark at all)
        letter_Grade = 'F';
    }
    return letter_Grade;
}

Now you would call it like so, using a return value:

letterGRADE = getLetterGrade(totalGrade);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文