[英]Member Data of Class not updating
我正在嘗試用C ++編寫NFL賽季模擬器,而這樣做的時候,在嘗試更新團隊記錄時遇到了問題。
我的代碼如下:
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(")");
}
div.h
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";
}
main.cpp中
#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
和totalLosses
變量都重置為0,我找不到原因。 我所做的所有調試均表明該過程完全按照我希望的方式進行,除了重置之外。 我將不勝感激為什么會發生這種情況。
如果要修改變量的屬性,則應將變量作為引用或指針傳遞。 未測試,但嘗試:
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");
構造函數Divison(Team one, Team two, Team three, Team four, std::string givenDivName)
創建了團隊的副本,因此原始對象不會更改。 以@Dimitry K.的建議為參考。
請在div中將“ team1,team2,team3和team4變量作為指針”,並在div的構造函數中為這些指針分配地址。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.