跨类的全局和局部函数和变量。怎么样?我做得对吗?

时间:2017-02-08 05:02:10

标签: c++ function class

当它应该小于零时,最终输出是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&lt; =但保持输出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_ */

1 个答案:

答案 0 :(得分:1)

您将combat_functions作为传递到combat_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。所以编译器不会误解。