hadi
hadi

Reputation: 26

function doesn't change object attributes in c++ even with reference

I'm trying to change a balls position by calling a function

    struct ball
    {
        int heading = north_east;
        bool visible = true;
        int pos_x = ball_start_x;
        int pos_y = ball_start_y; 
        char c = 'o';
    };

    void move_ball(ball *target_ball)
    {
        switch (target_ball->heading)
        {
            case north_east: 
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y -- ;
            }
            case north_west:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y -- ;
            }
            case south_west:
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y ++ ;
            }
            case south_east:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y ++ ;
            }
        }
    }
    
    
    int main(){
        ball ball_no_1;
        move_ball(&ball_no_1);
    }

but the position doesn't seem to change even though I call them by reference !

please help me...

Upvotes: 0

Views: 67

Answers (3)

Zack
Zack

Reputation: 117

Along with the missing declarations of heading (I suppose you want to use an enum, the alternative is to use #define to associate integers to symbols in the code, but it's not preferred), and values for positions, the error is the missing "break" in the cases.

Upvotes: 2

Sahadat Hossain
Sahadat Hossain

Reputation: 4351

you did not added the break after the cases, that is why it is going all the cases, and as it is going to all the cases ultimately changes nothing (adding and subtracting combined changes is 0).

void move_ball(ball *target_ball)
    {
        switch (target_ball->heading)
        {
            case north_east: 
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y -- ;
                break;
            }
            case north_west:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y -- ;
                break;
            }
            case south_west:
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y ++ ;
                break;
            }
            case south_east:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y ++ ;
                break;
            }
        }
    }

Upvotes: 3

sergiom
sergiom

Reputation: 4877

You forgot a bunch of break statements, all of the lines following the case north_east are executed, and the result of adding two and subtracting two is 0

switch (target_ball->heading)
        {
            case north_east: 
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y -- ;
                break;
            }
            case north_west:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y -- ;
                break;
            }
            case south_west:
            {
                target_ball->pos_x ++ ;
                target_ball->pos_y ++ ;
                break;
            }
            case south_east:
            {
                target_ball->pos_x -- ;
                target_ball->pos_y ++ ;
                break;
            }
        }

Upvotes: 5

Related Questions