【问题标题】:C++ functions won't return valuesC++ 函数不会返回值
【发布时间】:2016-05-02 05:07:18
【问题描述】:
#include<iostream>
#include<cstdlib>
#include<string>
#include<time.h>
using namespace std;

//Functions
// player strategy
int strategy(int user1Strat, int user2Strat);
// player total score per round
int currentScore();
// Display game result
void printResults();

int main()
{
    int total_player1 = 0; // player 1 current score
    int total_player2 = 0; // player 2 current score
    int player1_strat= 0;  //player 1 strategy for each turn
    int player2_strat = 0; // player 2 strategy for each turn

    // seed the random number generator.
    srand(static_cast<int> (time(NULL)));

    // get strategy for each player using functions <strategy>

    strategy(player1_strat, player2_strat);

    cout << player1_strat << endl << player2_strat << endl;

    system("pause");
    return 0;
}

int strategy(int user1Strat, int user2Strat)
{
    int x,
        y;

    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
    x = user1Strat;
    y = user2Strat;

    return x, y;
}

在函数main 中调用函数strategy 时,它会按应有的方式执行,但是一旦我要求返回值,它就会返回

Enter player1's roll until strategy: 10
Enter player2's roll until strategy: 5

0
0

press any key to contiue...

有谁知道为什么会发生这种情况或导致它的原因,是我在策略功能中的错误吗?还是在调用它时?

【问题讨论】:

  • 这不是在 C++ 中从函数返回多个值的方式。查找“通过引用返回”。
  • 您对从函数返回值的理解是有缺陷的。请阅读有关该主题的教科书。
  • 另外,您也不会从函数中获取任何返回值。 X 和 y 不会神奇地出现在您想要的一些随机变量上。基础教程/书籍将在这里为您提供帮助。
  • strategy 函数应该返回一个std::pair&lt;int,int&gt;,而不是单个int

标签: c++ function debugging


【解决方案1】:

strategy(player1_strat, player2_strat); 中的main() 在收到输入后不执行任何操作,因此您不会看到 player1_stratplayer2_strat 有任何变化。

如果你想修改strategy中的player1_stratplayer2_strat,你可以参考:

void strategy(int& user1Strat, int& user2Strat)
{
    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
}

或者你可以使用std::pair返回“多个值”:

//#include <utility>
std::pair<int, int> strategy(int user1Strat, int user2Strat)
{
    int x, y;

    cout << "Enter player1's roll until strategy: ";
    cin >> user1Strat;
    cout << "Enter player2's roll until strategy: ";
    cin >> user2Strat;
    x = user1Strat;
    y = user2Strat;

    return std::make_pair(x, y);
}

//main()
std::pair<int, int> result = strategy(player1_strat, player2_strat);

x = result.first;
y = result.second;

【讨论】:

    【解决方案2】:

    你只能从一个函数返回一个对象。 return x, y; 不返回 xy,而只返回 y。如果要更新多个变量,请将它们作为引用提供给函数,并在函数内部更改它们的值。

    编辑:

    正如@Keith Thompson 在 cmets 中提到的,逗号实际上是该语句中的运算符。它评估x(在那里没什么可做的),丢弃结果,然后评估并返回第二个参数y

    【讨论】:

    • return x, y; 是不允许的。它在语法上是有效的。它没有达到 OP 的预期。
    • @RSahu 你说得对,但并不准确。我将编辑我的答案。
    • 嗯,“return x, y”会返回 x 并将“y”本身视为另一个表达式吗?
    • 你应该提到逗号运算符。
    • @user3109672: return x, y; 等价于return(x, y); — 即评估x 并丢弃结果,然后评估y 并返回y 的值。通常情况下,x 的评估会有一些副作用;这不是,编译器可能会对此发出警告(至少在启用警告和/或优化的情况下)。
    猜你喜欢
    • 2011-02-14
    • 2013-03-25
    • 2017-08-12
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    相关资源
    最近更新 更多