简体   繁体   中英

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

I'm trying to change a ball s 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...

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;
            }
        }

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;
            }
        }
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM