jimbo999
jimbo999

Reputation: 80

Changing value of a class outside the class

i have the class Team which has the attribute "Points". In another sheet.cpp i want to change this points with some function like this:

void Result(Team a, Team b)    
{
int startWerte[] = { 8, 8 };//just random start values)
 std::vector< int > punkte(startWerte, startWerte + sizeof(startWerte) / sizeof(int));
points[0]=a.getpoints();
points[1]=b.getpoints();

Here follows some calculation which ends in the final values for the points stored in points2. Now i want to set them as the points of the teams, so they are stored.

a.setpoints(points[0])
b.setpoints(points[1]);

They are the correct values, but whenever this function ends the values are not stored in team.points correctly. If i do it by letting the Result function return the points2 vector to lets say vector testvector in the int main() it works. Example

vector<int> testvector;
testvector =Result(TeamA, TeamB) {//Same code than before follows
TeamA.setpoints(testvector[0];
TeamB.setpoints(testvector[1];

If i the repeat the Result-function everythin is stored correct. Is there no way to store the value for the points of the team class outside the int main ()?

Upvotes: 1

Views: 118

Answers (1)

riklund
riklund

Reputation: 1071

I think your problem is that you are passing Team by value rather than by reference.

Change the Result method to

void Result(Team & a, Team & b)

and everything should be fine.

Upvotes: 2

Related Questions