类的成员数据未更新 我试图在C++中编写NFL季节模拟器,在这样做时,我试图更新团队记录时遇到了问题。

类的成员数据未更新 我试图在C++中编写NFL季节模拟器,在这样做时,我试图更新团队记录时遇到了问题。,c++,C++,我的代码如下: team.h class Team { public: // Team Data Fields std::string teamName; // Team's Name int teamRating; // Team's Rating int winOrLose; // The result of the game: 1 - Win, 0 - Loss. int totalWins; // Total Wins

我的代码如下:

team.h

class Team
{
    public:

    // Team Data Fields 
    std::string teamName; // Team's Name
    int teamRating;  // Team's Rating
    int winOrLose;   // The result of the game: 1 - Win, 0 - Loss.
    int totalWins;   // Total Wins
    int totalLosses; // Total Losses

    // Class Constructors
    Team ();
    Team (std::string teamName);

    // Member functions definitions:
    void updateTeamRating();
    std::string teamRecord();
};

Team::Team()
{
    teamName = "Void";
    teamRating = 50;
    winOrLose = 0;
    totalWins = 0;
    totalLosses = 0;
}

Team::Team(std::string inputTeamName)
{
    teamName = inputTeamName;
    teamRating = 50;
    winOrLose = 0;
    totalWins = 0;
    totalLosses = 0;
}

void Team::updateTeamRating()
{
    if (winOrLose == 1)
    {
        teamRating = teamRating + 5;
        totalWins++;
    }
    else if (winOrLose == 0)
    {
        totalLosses++;
        teamRating = teamRating - 5;
        if (teamRating <= 0)
        {
            teamRating = 5;
        }
    }
}

std::string Team::teamRecord()
{
    return teamName + std::string(" (") + std::to_string(totalWins) + std::string("-") +
        std::to_string(totalLosses) + std::string(")"); 
}
class Divison
{
    public:

    std::string divName;

    // 4 Teams in a Divison
    Team team1;
    Team team2;
    Team team3;
    Team team4;

    Divison();
    Divison(Team one, Team two, Team three, Team four, std::string givenDivName);

    void game(Team team1, Team team2);
    void runRegDivSeason();

    Team playoffTeam();
    std::string toString();
};

Divison::Divison()
{
}

Divison::Divison(Team one, Team two, Team three, Team four, std::string givenDivName)
{
    team1 = one;
    team2 = two;
    team3 = three;
    team4 = four;
    divName = givenDivName;
}

void Divison::game(Team team1, Team team2)
{
    int totalRating = team1.teamRating + team2.teamRating;
    //cout << totalRating << endl;
    int result = (rand() % totalRating) + 1;
    if (result <= team1.teamRating)
    {
        team1.winOrLose = 1;
        team2.winOrLose = 0;
        team1.updateTeamRating();
        team2.updateTeamRating();
        std::cout << "Winner: " + team1.teamRecord() + " Loser: " + team2.teamRecord() + ".\n" << std::endl;
    }
    else
    {
        team1.winOrLose = 0;
        team2.winOrLose = 1;
        team1.updateTeamRating();
        team2.updateTeamRating();   
        std::cout << "Winner: " + team2.teamRecord() + " Loser: " + team1.teamRecord() + ".\n" << std::endl;
    }

}

void Divison::runRegDivSeason()
{
    for (int i = 0; i < 4; i++)
    {
        game(team1, team2);
        game(team1, team3);
        game(team1, team4);
        game(team2, team3);
        game(team2, team4);
        game(team3, team4);
    }
}

std::string Divison::toString()
{
    return divName + ":\n   " + team1.teamRecord() + "\n    "
        +  team2.teamRecord() + "\n " +  team3.teamRecord() + "\n   "
        +  team4.teamRecord() + "\n";
}
但我得到:

C:\myfiles\mfl>test
Winner: New England Patriots (1-0) Loser: Buffalo Bills (0-1).

Winner: New York Jets (1-0) Loser: New England Patriots (0-1).

Winner: New England Patriots (1-0) Loser: Miami Dolphins (0-1).

Winner: Buffalo Bills (1-0) Loser: New York Jets (0-1).

Winner: Miami Dolphins (1-0) Loser: Buffalo Bills (0-1).

Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).

Winner: Buffalo Bills (1-0) Loser: New England Patriots (0-1).

Winner: New York Jets (1-0) Loser: New England Patriots (0-1).

Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).

Winner: New York Jets (1-0) Loser: Buffalo Bills (0-1).

Winner: Buffalo Bills (1-0) Loser: Miami Dolphins (0-1).

Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).

Winner: Buffalo Bills (1-0) Loser: New England Patriots (0-1).

Winner: New England Patriots (1-0) Loser: New York Jets (0-1).

Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).

Winner: New York Jets (1-0) Loser: Buffalo Bills (0-1).

Winner: Miami Dolphins (1-0) Loser: Buffalo Bills (0-1).

Winner: New York Jets (1-0) Loser: Miami Dolphins (0-1).

Winner: New England Patriots (1-0) Loser: Buffalo Bills (0-1).

Winner: New England Patriots (1-0) Loser: New York Jets (0-1).

Winner: Miami Dolphins (1-0) Loser: New England Patriots (0-1).

Winner: Buffalo Bills (1-0) Loser: New York Jets (0-1).

Winner: Buffalo Bills (1-0) Loser: Miami Dolphins (0-1).

Winner: Miami Dolphins (1-0) Loser: New York Jets (0-1).

AFC East:
        New England Patriots (0-0)
        Buffalo Bills (0-0)
        New York Jets (0-0)
        Miami Dolphins (0-0)


C:\myfiles\mfl>

据我所知,这表明每个对象的
totalWins
totalosses
变量在每次游戏后都重置为0,我找不到原因。我所做的所有调试都表明,该过程完全按照我希望的方式进行,但重置除外。如果您能深入了解为什么会出现这种情况,我将不胜感激。

请在div中创建“team1、team2、team3和team4变量作为指针”,并在div的构造函数中为这些指针分配地址

请在div中使用“team1、team2、team3和team4变量作为指针”,并在div的构造函数中为这些指针分配地址

如果要修改变量的属性,应将变量作为引用或指针传递。 未测试,但请尝试:

Divison::Divison(Team& one, Team& two, Team& three, Team& four, std::string givenDivName)

并使用指针调用示例:

Divison AFCE = Divison(&Boston, &Buffalo, &NYJ, &Miami, "AFC East");

如果要修改变量的属性,则应将变量作为引用或指针传递。 未测试,但请尝试:

Divison::Divison(Team& one, Team& two, Team& three, Team& four, std::string givenDivName)

并使用指针调用示例:

Divison AFCE = Divison(&Boston, &Buffalo, &NYJ, &Miami, "AFC East");

构造函数
除法(团队一、团队二、团队三、团队四、std::string givenDivName)
创建团队的副本,因此不会更改原始对象。引用@Dimitry K.建议的引用。

构造函数
除法(第一组、第二组、第三组、第四组,std::string givenDivName)
创建团队的副本,因此不会更改原始对象。以@Dimitry K.建议的参考为例。

“我所做的所有调试表明,除重置外,该过程完全按照我所希望的方式进行。”此类调试会话是否表明,
分区
成员变量在执行
游戏
期间没有改变?由于您对这些对象的副本进行了修改,这些变量不会“重置”,也不会更改。如果要修改这些对象,请按引用传递,而不是按值传递。“我所做的所有调试都表明该过程完全按照我希望的方式进行,但重置除外。”此类调试会话是否表明,
分区
成员变量在执行
游戏
期间没有变化?由于您对这些对象的副本进行了修改,这些变量不会“重置”,也不会更改。如果你想修改这些对象,则要通过引用,而不是按值。因为这个问题是用C++标记的,使用引用会更习惯。因为这个问题是用C++标记的,使用引用会更习惯。
Divison AFCE = Divison(&Boston, &Buffalo, &NYJ, &Miami, "AFC East");