需要帮助将函数存储为变量并打印出变量

-1 c++ random function

我目前正在为我的 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;


}
Run Code Online (Sandbox Code Playgroud)

El *_*ino 7

您的代码示例在 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 */
}
Run Code Online (Sandbox Code Playgroud)

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

int diceroll() {
    int result ;

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

    return result ;
}
Run Code Online (Sandbox Code Playgroud)

现在,让我们看一下调用 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 */
}
Run Code Online (Sandbox Code Playgroud)

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

哦,如果您正在编译 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 ;
Run Code Online (Sandbox Code Playgroud)

最后,关于奇怪的:

<< + " "
Run Code Online (Sandbox Code Playgroud)

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

针对文字字符串的一元加号 (+)

  • `&lt;&lt; +" "` 不是错误,因为在这种情况下,`+` 不是二元 + 运算符,它是一元 + 运算符,当应用于字符串文字时,会强制它衰减为 `const char*` 。最终这变成了一个空操作,因为重载的`&lt;&lt;`可能会导致这种转换发生。这种一元 + 技巧有时用于强制 lambda 衰减为函数指针。也就是说,没有理由让该运算符存在,因为它除了让读者感到困惑之外什么也不做。 (2认同)