班级会员数据未更新

时间:2018-08-19 03:59:38

标签: c++

我正在尝试用C ++编写NFL赛季模拟器,而这样做的时候,在尝试更新团队记录时遇到了问题。

我的代码如下:

team.h

SELECT t1.*
       FROM transactions t1
       WHERE t1.origin_txn_id IS NOT NULL
              OR EXISTS (SELECT *
                                FROM transactions t2
                                WHERE t2.origin_txn_id = t1.txn_id)
       ORDER BY concat(coalesce(t1.origin_txn_id, ''), t1.txn_id);

div.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(")"); 
}

main.cpp

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";
}

当我运行程序时,我想要类似的东西:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>

#include "team.h"
#include "div.h"



int main()
{
    Team Boston = Team("New England Patriots");
    Team Buffalo = Team("Buffalo Bills");
    Team NYJ = Team("New York Jets");
    Team Miami = Team("Miami Dolphins");
    Divison AFCE = Divison(Boston, Buffalo, NYJ, Miami, "AFC East");
    AFCE.runRegDivSeason();
    std::cout << AFCE.toString() << std::endl;
    return 0;
}

但是我得到了

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 (1-1).

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

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

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

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

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

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

Winner: Miami Dolphins (2-2) Loser: New England Patriots (2-4).

Winner: New York Jets (4-1) Loser: Buffalo Bills (2-3).

Winner: Buffalo Bills (3-3) Loser: Miami Dolphins (2-3).

Winner: New York Jets (5-1) Loser: Miami Dolphins (2-4).

Winner: Buffalo Bills (3-3) Loser: New England Patriots (2-5).

Winner: New England Patriots (3-5) Loser: New York Jets (6-2).

Winner: Miami Dolphins (3-4) Loser: New England Patriots (3-6).

Winner: New York Jets (7-2) Loser: Buffalo Bills (3-4).

Winner: Miami Dolphins (4-4) Loser: Buffalo Bills (3-5).

Winner: New York Jets (8-2) Loser: Miami Dolphins (4-5).

Winner: New England Patriots (4-6) Loser: Buffalo Bills (3-6).

Winner: New England Patriots (5-6) Loser: New York Jets (8-3).

Winner: Miami Dolphins (5-5) Loser: New England Patriots (4-7).

Winner: Buffalo Bills (4-6) Loser: New York Jets (8-4).

Winner: Buffalo Bills (5-6) Loser: Miami Dolphins (5-6).

Winner: Miami Dolphins (6-6) Loser: New York Jets (8-5).

AFC East:
        New England Patriots (4-7)
        Buffalo Bills (5-6)
        New York Jets (8-5)
        Miami Dolphins (6-6)


C:\myfiles\mfl>

据我所知,这表明每次游戏后每个对象的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变量都重置为0,我找不到原因。我所做的所有调试均表明该过程完全按照我希望的方式进行,除了重置之外。我将不胜感激为什么会发生这种情况。

3 个答案:

答案 0 :(得分:3)

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

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

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

并使用指针调用示例:

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

答案 1 :(得分:1)

构造函数Divison(Team one, Team two, Team three, Team four, std::string givenDivName)创建团队的副本,因此原始对象不会更改。引用@Dimitry K.的建议。

答案 2 :(得分:0)

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