【问题标题】:Needing help storing functions as variables and printing out the variables需要帮助将函数存储为变量并打印出变量
【发布时间】:2021-09-06 12:32:02
【问题描述】:

我目前正在为我的 CS121 课程开发一个掷骰子程序,这是我的代码。我正在尝试编写一个滚动 6 面骰子并在滚动 1 时重新滚动它的函数。我想调用该函数 3 次,然后将函数的输出加在一起。不幸的是,当我尝试添加它并输出它时,roll2 下方有一条红线,当我将鼠标悬停在它上面时,它指出“表达式必须具有整数或无范围枚举类型。我很难过,所以如果你能帮我找到解决方法那太好了。

#include <iostream>
using namespace std;
void diceroll() {
    int result = ((rand() % 6) + 1);
    if (result != 1)
       cout << result << + " ";
    else cout << result << +" ";
}
int main() {

    auto roll1 = diceroll;
    auto roll2 = diceroll;
    auto roll3 = diceroll;



    cout << "The total of the 3 numbers is" << endl;
    cout << roll1 + roll2 + roll3;


}

【问题讨论】:

  • 你已经定义了void diceroll,这意味着编译器认为它没有返回值。该函数也没有返回值 - 它只是将一个值打印到控制台。将其更改为返回 int,然后在代码中返回 return result;。此外,在diceroll 中,您的if 声明毫无意义,因为ifelse 做同样的事情——它们都是cout &lt;&lt; result &lt;&lt; " ";
  • +" " 语法值得在 Stackoverflow 上提出自己的问题。那是什么? if 声明应该完成什么? ifelse 部分的功能完全相同。
  • 当您使用auto 关键字时,您让编译器决定roll1roll2roll3 的类型。如果您希望它们是整数,请将它们定义为整数(例如int roll1 = diceroll;)。这将更改您的错误,以便更准确地描述错误所在的位置。

标签: c++ function random


【解决方案1】:

您的代码示例在 C++ 中的语法不正确。

查看原始代码示例:

#include <iostream>
using namespace std;

void diceroll() {
    int result = ((rand() % 6) + 1);
    if (result != 1)
       cout << result << + " ";   /* Hmmm...: why add the '+' ? */
    else cout << result << +" ";  /* Hmmm...: why add the '+' ? */
}
int main() {

    auto roll1 = diceroll;
    auto roll2 = diceroll;
    auto roll3 = diceroll;

    cout << "The total of the 3 numbers is" << endl;
    cout << roll1 + roll2 + roll3; /* ERRORS: adding pointers, operator precedence */
}

让我们从第一个任务开始:一个掷骰子并返回结果的函数,并附加要求如果掷出“1”,则必须再次掷骰。换句话说,它永远不应该返回 '1'。

int diceroll() {
    int result ;

    do {
        result = ((rand() % 6) + 1);
    } while ( result == 1 ) ;

    return result ;
}

现在,让我们看看调用 3 次并输出结果。

int main() {

    /* Option 1: explicit */
    cout << "The total of the 3 numbers is" << endl;
    cout << ( diceroll() + diceroll() + diceroll() ) << endl ;

    /* Option 2: using variables containing function pointers */
    auto roll1 = diceroll ; // assigns function pointer
    auto roll2 = diceroll ;
    auto roll3 = diceroll ;
    
    cout << "The total of the 3 numbers is" << endl;
    cout << ( roll1() + roll2() + roll3() ) << endl ;

    /* Option 3: In case you were really trying to do this: */
    auto roll1 = diceroll() ; // assigns result of dice roll
    auto roll2 = diceroll() ; // assigns result of dice roll
    auto roll3 = diceroll() ; // assigns result of dice roll

    cout << "The total of the 3 numbers is" << endl;
    cout << ( roll1 + roll2 + roll3 ) << endl ;

    return 0 ; /* EXIT_SUCCESS */
}

在所有情况下,请记住将总和捆绑在括号内,否则您会被运算符优先级所困扰...

哦,如果您正在编译 C++11 之前的版本:

// ... declare the function pointer type (underneath the 'using namespace std' statement)
typedef int (*diceroll_function_t)() ;

// ... Inside main(): instead of auto roll1 = diceroll 
diceroll_function_t roll1 = diceroll ;

最后,关于怪事:

<< + " "

Kyle 在下面评论了该部分编译的原因。这是一个讨论它的链接:

Unary plus (+) against literal string

【讨论】:

  • &lt;&lt; +" " 不是错误,因为在这种情况下,+ 不是二元 + 运算符,它是一元 + 运算符,当应用于字符串文字时,它会强制它衰减为 @ 987654329@。最终这变成了无操作,因为重载的&lt;&lt; 可能会导致这种转换发生。这种一元 + 技巧有时用于强制 lambda 衰减为函数指针。也就是说,没有理由让那个操作符在那里,因为它除了让读者感到困惑之外什么都不做。
【解决方案2】:

我相信这应该可以完成这项工作。首先我们将diceroll()函数的返回类型更改为int,这样我们就可以在我们的主函数中调用它并获取骰子表面的值。使用直观的递归,我制定了函数来打印骰子表面的值。

我建议使用 srand(time(0)) 播种 rand 函数,否则每次运行程序时,你都会在骰子的面上得到相同的值。

每个骰子都会继续调用自己,直到 result 的值不是 1。

#include <iostream>
using namespace std;
int diceroll()
{
    int result = ((rand() % 6) + 1);
    cout << result << +" ";
    if (result == 1)
        return diceroll();
    cout << endl;
    return result;
}
int main()
{
    srand(time(0));
    int r1 = diceroll(), r2 = diceroll(), r3 = diceroll();
    cout << "The total of the 3 numbers is: "
         << (r1 + r2 + r3) << endl;
}

【讨论】:

    【解决方案3】:

    roll1 + roll2 + roll3 中,您不是在调用函数,而是在进行指针运算。将() 添加到您的函数名称以调用它们。

    【讨论】:

    • OP还需要return函数中的一个值。
    • 我认为你读错了代码。这不是发布代码的主要问题。
    • 没错,但他还需要调用函数:我想调用那个函数3次
    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多