当它应该小于零时,最终输出为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]。您当前的代码包含很多与您的问题相关的代码 - 通常,最小样本看起来与单元测试相似:只执行一项任务,输入值指定为可重现性。 –