Update a class property via setter doesn't work

Aug 18, 2015 at 2:00pm
Hello,

I'm trying to exercise c++ coding working on an old style text adventure.
I have created a class for Characters.

i store the created characters in a vector.
 
static vector<Character> charList;


now the class Character have a method that change the Room position of the character.

 
void setRoom(int ir) {_inRoom=ir;}


why if i set the charList[i].setRoom(NewRoom) the character retain the room number with which was initialed in the constructor?

Thank you very much
Last edited on Aug 18, 2015 at 2:03pm
Aug 18, 2015 at 2:15pm
This is too small of a snippet for me to be able to tell you why Character::setRoom is not working.
Aug 18, 2015 at 2:24pm
Here's the class code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Character{

  public:
    string _name, _genre="";
    int _inRoom, _status=0;
    bool _activeCharacter=false;
    
    string getName () {return (_name);}
    int getRoom() {return (_inRoom);}
    void setRoom(int ir) {_inRoom=ir;}

};

Character::Character (vector<CObject> my_vector, string name, string genre, int inRoom, bool activeCharacter, int status) {
  _name=name;
  _genre = genre;
  _inRoom=inRoom;
  _activeCharacter = activeCharacter;
  _status=status;
  objects=my_vector;
  My_Objects.clear();

}



in the main()
1
2
3
4
5
6
7
8
Character myCharacter(Obj, "Pluto", "Alien", 4, true, 1);
//note the 4 is the room.

charList.push_back(myCharacter)

myCharacter[i].setRoom(3); //work, but if i later call

int myCharRoom=myCharacter[i].getRoom(); 


i get 4 as a result

Last edited on Aug 18, 2015 at 2:39pm
Aug 18, 2015 at 2:37pm
There is nothing wrong with setRoom().

Show us where you modify an instance of Character that setRoom() doesn't work.

Apparently unrelated, but of concern:
Line 14: my_vector is passed by value. When you store it in objects at line 20, you're storing a copy. Any changes made to objects won't be reflected in my_vector, or the vector that was passed to the constructor.

Aug 18, 2015 at 3:45pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static void charactersMoving(vector<CCharacter> charList) {

    string name;
    int aRoom=0;
    int roomExit;
for (int i=0; i< charList.size(); i++) {

    if (charList[i]._activeCharacter==true) {
    // skip i don't want to move the active character to another room, but only    the inactive character
    }else{
        name=charList[i].getName();
        aRoom=charList[i].getRoom();
        roomExit=GetRoomExit(); //provide a random possible exist

     }
            charList[i].setRoom(randomExit);
}


}



as for the suggestion on line 14, could you please explain it better?
Last edited on Aug 18, 2015 at 3:58pm
Aug 18, 2015 at 5:49pm
Line 14: my_vector is passed by value. When you store it in objects at line 20, you're storing a copy. Any changes made to objects won't be reflected in my_vector, or the vector that was passed to the constructor.

Possibly good for the constructor, but definitely bad for the charactersMoving function. When you modify copies in the charactersMoving function, the originals someplace else never reflect the changes that were made to the copy.
http://courses.washington.edu/css342/zander/css332/passby.html
Aug 18, 2015 at 6:02pm
Thank you for the infos.

What about this?

"why if i set the charList[i].setRoom(NewRoom) the character retain the room number with which was initialed in the constructor?"

really struggling with this.

Aug 18, 2015 at 11:02pm
Amiplus wrote:
"why if i set the charList[i].setRoom(NewRoom) the character retain the room number with which was initialed in the constructor?"

really struggling with this.
kevinkjt2000 wrote:
When you modify copies in the charactersMoving function, the originals someplace else never reflect the changes that were made to the copy.

So, when you modify the original in the constructor, you keep the changes because it is original and not a passed by value copy.
Last edited on Aug 18, 2015 at 11:03pm
Aug 19, 2015 at 6:39am
Thank you.

How to solve this?
Aug 19, 2015 at 6:55am
What is the difference of by value and by reference function arguments?
Aug 19, 2015 at 8:05am
Could you please show a modified version of my routine in order to make it work?
Last edited on Aug 19, 2015 at 10:23am
Aug 19, 2015 at 11:28am
No. It would hurt you.

Read this tutorial page: http://www.cplusplus.com/doc/tutorial/functions/

After reading that, can you answer my question?

Then, when you can answer my question, can you apply your new knowledge to your problem?
Aug 19, 2015 at 12:10pm
Look at this code with the intent to enlarge your understanding.
Doing that has never hurt anyone; it won't hurt you either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <vector>
#include <string>

struct character { int room ; character( int r = 0 ) : room(r) {} };

void pass_by_value( std::vector<character> players )
{ for( character& c : players ) c.room = 99 ;  }

void pass_by_reference( std::vector<character>& players )
{ for( character& c : players ) c.room = 99 ;  }

std::vector<character> pass_by_value_return_modified_object( std::vector<character> players )
{
    for( character& c : players ) c.room = 99 ;
    return players ;
}

void print( const std::vector<character>& players )
{
    for( character c : players ) std::cout << c.room << ' ' ;
    std::cout << '\n' ;
}

int main()
{
    {
        std::cout << "------  pass by value ------\n" ;
        std::vector<character> players { 0, 1, 2, 3, 4, 5 } ;
        print(players) ;
        pass_by_value(players) ;
        print(players) ;
    }


    {
        std::cout << "\n------  pass by reference ------\n" ;
        std::vector<character> players { 0, 1, 2, 3, 4, 5 } ;
        print(players) ;
        pass_by_reference(players) ;
        print(players) ;
    }

    {
        std::cout << "\n------  pass by pass by value, return_modified_object------\n" ;
        std::vector<character> players { 0, 1, 2, 3, 4, 5 } ;
        print(players) ;

        std::cout << "ignore the result: " ;
        pass_by_value_return_modified_object(players) ; // ignore the result
        print(players) ;

        std::cout << "update object with the result: " ;
        players = pass_by_value_return_modified_object(players) ; // update object with the result
        print(players) ;
    }
}

http://coliru.stacked-crooked.com/a/5641232f4a9f01e9
Aug 20, 2015 at 12:57pm
Thank you for your example: i have to study it, cause it's a little different from my character class using getter and setter.

Aug 20, 2015 at 1:43pm
It works now, passing by reference.

Thank you to all. I've read again the tutorial suggested.

Btw, why other settings that i change without passing by reference, but only by values are working?

just for example:

charList.at(i)._activeCharacter=false is working.

This is why i don't call a setter that reside in the Character class, but i set directly the vector<Character> charList in the main()?



Aug 20, 2015 at 8:12pm
you need to declare that Character constructor with those parameters in your class. If none is declared, a default (no argument) constructor will be created by the compiler. This is one thing wrong with your code.
Last edited on Aug 20, 2015 at 8:14pm
Aug 21, 2015 at 8:22am
Parameters are configured
 
Character::Character (vector<CObject> my_vector, string name, string genre, int inRoom, bool activeCharacter, int status) {
Topic archived. No new replies allowed.