2017-02-08 21 views
-2

当它应该小于零时,最终输出为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_ */ 
+2

您是否尝试过使用您的调试器? –

+1

'playerFight'通过值取其参数,这意味着它会复制两个'combat_functions'实例并修改这些副本。 “里克”保持不变。 –

+0

欢迎来到Stack Overflow!请[编辑]你的代码,以减少它到你的问题[mcve]。您当前的代码包含很多与您的问题相关的代码 - 通常,最小样本看起来与单元测试相似:只执行一项任务,输入值指定为可重现性。 –

回答

0

你传入combat_functionscombat_functions::playerFight(),而应该把它作为参考

当您将参数作为值传递时,将会生成一个变量副本。当您在函数中修改变量时,实际上您正在修改它的副本,而不是原始变量。

做正确的方法,是用参考而不是值:

void playerFight(combat_functions& player1, combat_functions& player2); 

或者使用指针

void playerFight(combat_functions *player1, combat_functions *player2); 

你的代码的另一件事,全局变量在定义为combatfunctions.cpp似乎没有必要。而playerFight()更改类中的变量,而不是全局变量。如果你真的想改变全局变量(虽然我不认为这是你想要的),你应该使用::health,::attack ..来明确告诉编译器。或者更好的方法,将全局变量的名称更改为g_healthg_attack。所以编译器不会误解。

+0

谢谢你的帮助! – hevansa98

+0

@ user7532445如果解决了您的问题,您可以接受我的答案。 :-) –