0

As title says, i have this code here

#include <iostream>
#include <string>
using namespace std;

class SpaceShip{
public:
    int fuel;
    SpaceShip(int f){
        this->fuel=f;
    }
    bool ConsumeFuel(int f){
        cout<<"Consuming "<<f<<" fuel\n";
        if (this->fuel<f){
            this->fuel=0;
            cout<<"Out of Fuel\n";
            return false;
        }
        else{
            this->fuel=this->fuel-f;
            if (this->fuel==0){
                cout<<"Out of Fuel\n";
            }
            return true;
        }
    }
    void PrintResources(){
        cout<<"Available: "<<this->fuel<<"\n\n";
        return;
    }
};

class MissionSelector{

public:
    bool select(SpaceShip sps){
        while(true){

            cout<<"Choose exploration\n\n";
            string id;
            cin>>id;
            if(id=="exploration"){
                return this->exploration(sps);
            }
            else{
                cout<<"No valid id selected!\n";
            }

        }
    }
private:
    bool exploration(SpaceShip sps){
        bool result=sps.ConsumeFuel(20);
        if (result){
            cout<<"Mission completed correctly!\n\n";
        }
        else{
            cout<<"Mission Failed, not enough resources!\n\n";
        }
        return result;
    }

};
int main(){

    MissionSelector selector;
    SpaceShip sps(100);
    sps.ConsumeFuel(20);
    bool end=false;
    int score=0;
    while(!end){
        sps.PrintResources();
        if(selector.select(sps)){
            score++;
        }
        else{
            end=true;
        }
    }
    return 0;
}

When i call ConsumeFuel(x) frome the main it edits the object's fuel variable just fine, when i do it from the exploration method of the MissionSelector class (which i call in the select method of the MissionSelector, which is called in the main) it returns true but never edits the fuel variable.

There are other parts of the code i omitted since it's just other details not really helping in finding out the bug which is here, it's mostly other resources and other missions. And stackoverflow keeps telling me there are not enough details since it's mostly code but it's really everything here, nothing more i'll add this couple of lines to make it happy.

Can someone help me here? Really thanks!

1 Answers1

3

You need to declare the parameters as references, because otherwise you will be passing a copy instead of the original object, any change wont be reflected, until you receive references:

bool exploration(SpaceShip& sps){ ... }
bool select(SpaceShip& sps){ ... }
Jans
  • 11,064
  • 3
  • 37
  • 45
  • Thanks, i would have never guessed! Coming from Java i never thought about having to pass a class by reference – Giuseppe Ayanami De Frenza Dec 04 '18 at 21:39
  • 2
    @GiuseppeAyanamiDeFrenza In that case, let's forestall what is likely to be the next nasty surprise: [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) and provide the ideology that makes C++'s resource management "work": [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) – user4581301 Dec 04 '18 at 22:31