(C ++)我的代码有问题,我不知道为什么会发生这种情况。请帮忙。
(编辑)这是GamePlay.h,我认为将来对任何人来说都是有必要的:
#ifndef GAMEPLAY_H
#define GAMEPLAY_H
#include "Card.h"
// #include "Player.h"
#include "Sensei.h"
#include <iostream>
#include <string>
using namespace std;
class GamePlay
{
private:
Card playerCards[5];
Card computerCards[5];
int arr[3];
public:
GamePlay(Sensei, Player);
int checkRoundWinner(int);
void showCards();
};
#endif
这是我的GamePlay课程:
#include
#include "GamePlay.h"
#include
using namespace std;
GamePlay::GamePlay(Sensei s, Player username)
{
// Card playerCards[5];
// Card computerCards[5];
//////////////////////////////////TEST
int arr[3];
for(int i = 0; i < 3; i++) //Assign test array
{
arr[i] = i*2;
}
cout << "___FIRST PRINT___" << endl;
for(int i = 0; i < 3; i++) //Print test array
{
cout << arr[i] << endl;
}
//////////////////////////////////TEST
// for(int i = 0; i < 5; i++) //Assign the arrays
// {
// playerCards[i] = s.getCards(username.getLevel(), i);
// computerCards[i] = s.getCards(username.getLevel(), i);
// }
}
int GamePlay::checkRoundWinner(int a) //Takes user chosen card...Compares card to the computer's chosen card
{
cout << "___SECOND PRINT___" << endl;
for(int i = 0; i < 3; i++) // Print test array again
{
cout << arr[i] << endl;
}
// playerCards[4].displayCard();
// int random = rand()%5;
// Card emptyCard("E",0);
// Card chosenCard = playerCards[a-1];
// cout << "Should be chosen card: ";
// playerCards[a-1].displayCard();
// cout << "Chosen card: ";
// chosenCard.displayCard();
// Card computerCard = computerCards[random];
// int chosenCardPower;
// int computerCardPower;
// if(chosenCard.getPowerLevel() != 0)
// {
// chosenCardPower = chosenCard.getPowerLevel();
// }else
// return 0; //Will have to check inside main if this 0 is returned (If the chosen card has already been used)
// while(computerCard.getElement() == "E")
// {
// random = rand()%5;
// computerCard = computerCards[random];//A random card from computerCards by using Math random function between 0-5
// }
// cout << "\nHere is your card: ";
// chosenCard.displayCard();
// cout << "\nHere is the computer's card: ";
// computerCard.displayCard();
// computerCardPower = computerCard.getPowerLevel();
// if(computerCardPower == chosenCardPower)
// {
// return 3;
// }
// else if(computerCardPower > chosenCardPower)
// {
// computerCards[random] = emptyCard;//Set that card in the computerCards array to an empty card
// return 2;
// }
// else
// {
// playerCards[a-1] = emptyCard;//Set that card in the playerCards array to an empty card
// return 1;
// }
}
该类具有自己的功能,但是我遇到的问题是,每当我尝试在构造函数之外使用它们时,似乎我的Card数组(playerCard和computerCard)都在更改。
为了检验该理论,我在构造函数内部制作了一个测试数组,并为其分配了变量。然后,我在构造函数中打印出了整个数组。然后,我再次在构造函数的外部(在checkRoundWinner方法内部)打印出整个数组。这是打印的内容:
___FIRST PRINT___
0
2
4
___SECOND PRINT___
6
32764
27533224
在第二次打印之前,我没有对数组进行任何更改。如果您想查看我的Driver类,这里是:
#include
#include "Sensei.h"
#include "GamePlay.h"
#include "Player.h"
#include
#include
#include
#include
using namespace std;
int main()
{
Sensei mySensei;
Player newPlayer("Bob", "Bobb");
mySensei.welcomePlayer(newPlayer.getFirst(), newPlayer.getLast());
mySensei.readPack(); //Reads a card pack and assigns them to the arrays inside of mySensei
newPlayer.setLevel(1); //Changes the player level
//HERE IS WHEN THE PROBLEM OCCURS
GamePlay newGame(mySensei, newPlayer); //Make a new GamePlay (call the constructor)
int num = newGame.checkRoundWinner(1); //Call checkRoundWinner inside that GamePlay class and store the returned value
// if(num == 1)
// cout << "Comp wins." << endl;
// else if(num == 2)
// cout << "Player wins." << endl;
// else if(num == 3)
// cout << "TIE" << endl;
// else if(num == 0)
// cout << "CHANGE CARD" << endl;
}
谢谢您的时间!
答案 0 :(得分:3)
在构造函数中
GamePlay::GamePlay(Sensei s, Player username)
{
// Card playerCards[5];
// Card computerCards[5];
//////////////////////////////////TEST
int arr[3];
//…
有一个声明为本地数组,该数组在退出构造函数后将不活动。
例如,成员函数checkRoundWinner
不处理构造函数中声明的数组。
int GamePlay::checkRoundWinner(int a) //Takes user chosen card...Compares card to the computer's chosen card
{
cout << "___SECOND PRINT___" << endl;
for(int i = 0; i < 3; i++) // Print test array again
{
cout << arr[i] << endl;
}
如果该类具有数据成员arr
,则应删除本地数组的声明。
int arr[3];
来自构造函数。