简体   繁体   中英

Global and local functions and variables across classes. How? Am I doing it right?

The final output is 12 when it should be less than zero and I think it is because the global variable is not being changed. Am I right? What would I need to change?

central.cpp (MAIN FILE)

/*
 * 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() should output a value 0<= but keeps outputting 12: the original value

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_ */

You are passing combat_functions as value into combat_functions::playerFight() , while you should pass it as reference .

When you pass parameters as value, a copy of variable will be generated. When you modify the variable in the function, actually you are modifying a copy of it, not the original variable.

The right way to do it, is to use reference rather than value:

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

Or use pointer :

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

Another thing about your code, the global variables defined in combatfunctions.cpp seems not necessary. And playerFight() changes variables in class, not global variables. If you really want to change global variables (although I don't think that's what you want), you should use ::health , ::attack .. to explicitly tell compiler. Or a better way, to change global variables' name to g_health , g_attack . So compiler won't misunderstand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM