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

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


}

回答

您的代码示例在 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 在下面评论了该部分编译的原因。这是一个谈论它的链接:

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

  • The `<< +" "` is not an error because the `+` is not the binary + operator in this case, it's the unary + operator and when applied to the string literal forces it to decay into a `const char*`. Ultimately this becomes a no-op since the overloaded `<<` may cause this conversion to happen anyway. This unary + trick is sometimes used to force lambdas to decay into function pointers. That said, there's no reason to have that operator there as it doesn't do anything except confuse the reader.

以上是需要帮助将函数存储为变量并打印出变量的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>