Home > Net >  Value not updating in class C [duplicate]
Value not updating in class C [duplicate]

Time:10-05

I'm writing an assignment program which implements OOP principle which is meant to simulate an election.

I have a class Party:

class Party{
public:
    Party(){};
    ~Party(){};
    Party(std::string name, int budget){partyName = name; electionBudget = budget;}
    std::string getPartyName(){return partyName;}
    std::string getLeaderName(){return leaderName;}
    std::string getManagerName(){return managerName;}

    int getElectionBudget(){return electionBudget;}
    void setElectionBudget(int budget){electionBudget = budget;}

    void setLeaderName(std::string lName){leaderName = lName;}
    void setManagerName(std::string mName){managerName = mName;}

    std::vector<Stance> getPartyStances(){return partyStance;}
    void setPartyStances(std::vector<Stance> pStance){partyStance = pStance;}

    void *leaderPtr;
    void *managerPtr;

    void setLeader(void *a){leaderPtr = a; }
    void setManager(void *a){managerPtr = a;}

    void spendMoneyOnEvent(int cost){
        int currentBudget = getElectionBudget();
        setElectionBudget(currentBudget - cost);
    }


    void print();

private:
    std::string partyName;
    std::string leaderName;
    std::string managerName;
    std::vector<Stance> partyStance;
    int electionBudget;
};

In a big for loop, every day I update the budget each party has left like this:

for(Party p : partiesVector){
            std::cout << p.getPartyName() << " budget remaining: " << "$" << p.getElectionBudget() << std::endl;
        }

I also have the code implemented in my main.cpp to update the budget every time an event happens. For example:

case 0:
        std::cout << debate.getEventName() << std::endl;
        std::cout << debate.getEventDecription() << std::endl;

        party1.spendMoneyOnEvent(500);

        std::cout << party1.debate.haveDebate(party1, party2, party3);
        break;

However, no matter what I try with setting of things or trying to update the variable, it always remains the same (what I declared it as). Is there something I'm not understanding or missing here?

Any help would be greatly appreciated, since I'm still relatively new to C

Thanks!

CodePudding user response:

for(Party p : partiesVector) is receiving the Party object by value, thus a copy made. Any updates to p will update the copy, not the original. Make sure such loops receive the Party by reference instead:

for(Party &p : partiesVector)

You should do this anyway, even for read-only loops.

More importantly, your display loop is iteration through a vector of Party objects, but your update code is directly updating a Party object that is not in the vector. Likely you made another copy when you added the Party to the vector. So you should change your update code to refer to the Party in the vector.

partiesVector[someIndex].spendMoneyOnEvent(500);

Or else change the vector to hold pointers to Party objects you have created external to the vector.

vector<Party*> partiesVector;
...
Party party1;
...
partiesVector.push_back(&party1);
...
party1.spendMoneyOnEvent(500);
...
for(Party *p : partiesVector) {...}
  • Related