當它應該小於零時,最終輸出爲12,我認爲這是因爲全局變量沒有被更改。我對嗎?我需要改變什麼?全局和局部函數和跨類變量。怎麼樣?我做對了嗎?
central.cpp(主文件)
/*
* central.cpp
*
* Created on: Feb 7, 2017
* Author: Harry Evans Allen, IV
*/
#include <iostream>
#include <string>
#include "combatfunctions.h"
using namespace std;
int main(void){
string name;
cout << "Hello, Welcome to Silver Stone.\nWhat is your name?\n";
cin >> name;
combat_functions mainC(name, 15,5,3,3);
combat_functions Rick("Rick", 12,4,3,3);
mainC.playerFight(mainC,Rick);
cout << Rick.getHP();
}
Rick.getHP()應輸出的值0 < =但保持輸出12:原始值
combatfunctions.cpp
/*
* combatfunctions.cpp
*
* Created on: Feb 7, 2017
* Author: ted
*/
#include "combatfunctions.h"
#include <iostream>
#include <string>
using namespace std;
int health, attack, defense, movement;
string name;
combat_functions::combat_functions(string callsign, int hp, int power, int shield, int mobility){
health=hp;
attack=power;
defense=shield;
movement=mobility;
name=callsign;
}
int combat_functions::getHP(){
return health;
}
int combat_functions::getAttack(){
return attack;
}
int combat_functions::getDefense(){
return defense;
}
int combat_functions::getMobility(){
return movement;
}
string combat_functions::getName(){
return name;
}
void combat_functions::setHP(int increment){
health+=increment;
}
void combat_functions::setAttack(int increment){
attack+=increment;
}
void combat_functions::setDefense(int increment){
defense+=increment;
}
void combat_functions::setMobility(int increment){
movement+=increment;
}
bool combat_functions::isAlive(combat_functions player){
if(player.getHP()>=1){
return 1;
}else{
return 0;
}
}
void combat_functions::playerFight(combat_functions player1, combat_functions player2){ //This functions lets the players take from eachother's value
while(isAlive(player1)&&isAlive(player2)){
if(player2.getDefense()<player1.getAttack()){
player2.setHP(-1*(player1.getAttack()-player2.getDefense())); //Should change the global value of hp
}
if(player1.getDefense()<player2.getAttack()){
player1.setHP(-1*(player2.getAttack()-player1.getDefense())); //Should change the global value of hp
}
if(!isAlive(player1)){
cout << player1.getName() << " has died."<<endl;
}else if(!isAlive(player2)){
cout << player2.getName() << " has died."<<endl;
}else{
cout << player1.getName() << " " << player1.getHP() << "\n" <<player2.getName() << " " << player2.getHP() << endl;
}
}
}
combatfunctions.h
/*
* combatfunctions.h
*
* Created on: Feb 7, 2017
* Author: ted
*/
#ifndef COMBATFUNCTIONS_H_
#define COMBATFUNCTIONS_H_
#include <string>
using namespace std;
class combat_functions {
public:
combat_functions(string, int, int, int, int);
int getHP();
int getAttack();
int getDefense();
int getMobility();
string getName();
void setHP(int);
void setAttack(int);
void setDefense(int);
void setMobility(int);
bool isAlive(combat_functions);
void playerFight(combat_functions, combat_functions);
private:
int health, attack, defense, movement;
string name;
};
#endif /* COMBATFUNCTIONS_H_ */
您是否嘗試過使用您的調試器? –
'playerFight'通過值取其參數,這意味着它會複製兩個'combat_functions'實例並修改這些副本。 「裏克」保持不變。 –
歡迎來到Stack Overflow!請[編輯]你的代碼,以減少它到你的問題[mcve]。您當前的代碼包含很多與您的問題相關的代碼 - 通常,最小樣本看起來與單元測試相似:只執行一項任務,輸入值指定爲可重現性。 –