-1

I have the following classes:

//----------------------------------------------------------------------
//                              CHARACTER CLASS
//----------------------------------------------------------------------
class Character
{
private:
    POINT2f char_position;

public:
    VECTOR2f char_velocity;

    Character();
    ~Character();

    // Mutators
    void SetChar_Position(POINT2f p);
    void SetChar_Velocity(VECTOR2f v);

    // Accessors
    POINT2f GetChar_Position();
    VECTOR2f GetChar_Velocity();
};

//----------------------------------------------------------------------
//          PLAYER CLASS - INHERITED FROM "CHARACTER" CLASS
//----------------------------------------------------------------------
class Player : public Character
{
private:

public:
int health;

Player();
~Player();

void SetPlayer_Health(int h);

int GetPlayer_Health();
};

So essentially the player class inherits from the character class.

The Character class has member function which lets you set the characters position and velocity, which take in a POINT2f and VECTOR2f.

Within my main bit of code I create a player character Player Player1 and than set the balls velocity and position if a button is pressed:

if (ui.isActive('A'))   // Check to see if "A" key is pressed
    {
        // Player Ball
        Player1.SetChar_Velocity.x = -12.0;
        Player1.SetChar_Position.x += (Player1.GetChar_Velocity.x*dt);
        Player1.SetChar_Velocity.x += (acceleration*dt);
    }

I'm currently getting errors that say left of .x must have class/struct/union, so I change Player1.SetChar_Velocity.x = -12.0; to Player1.SetChar_Velocity().x = -12.0; which than brings up the error expression must have class type and to few arguments in function call.

Does anyone know of a method that will allow my to only manipulate the x number of the char_velocity only. also I understand that my code to Set the .x velocity isn't correct because I'm not passing in a VECTOR2f value.

Nick
  • 57
  • 8
  • `Player1.char_velocity.x += ...`? By the way your `SetChar_Velocity` and `GetChar_Velocity` functions are redundant as long as `char_velocity` is public. – user253751 Sep 29 '16 at 02:55
  • That's a good point, I just like having function because it's a little neater in my opinion. Well how's about if I want to change the x values of `char_position` which is located in private? – Nick Sep 29 '16 at 02:59
  • Then you'll have to do `POINT2f pos = Player1.GetChar_Position(); pos.x += ...; Player1.SetChar_Position(pos);`. Hardly convenient! – user253751 Sep 29 '16 at 03:06
  • Fair point, well thanks for all the info. – Nick Sep 29 '16 at 03:12

1 Answers1

0

In order to achieve what you want to do, you will want the return type of you Get_ methods to be a reference (wiki). Your code will then look like

class Character
{
private:
    POINT2f char_position;

public:
    VECTOR2f char_velocity;

    // Accessors
    POINT2f& GetChar_Position() { return char_position; }
    VECTOR2f& GetChar_Velocity() { return char_velocity; }
};

Also, it is often advised or required to be able to preserve const-correctness in your code, so you might want to add

const POINT2f& GetChar_Position() const { return char_position; }
const VECTOR2f& GetChar_Velocity() const { return char_velocity; }

This will allow you to do some calls like

POINT2f current_pos = a_char.GetChar_Position(); // const 
a_char.GetChar_Velocity().x += 2;                // non-const

Please note that it is often advised to pass the structures as const references instead of copies (although this might not hold true depending on your c++ version and your types, you can see this question for clarification), hence change your

void SetChar_Position(POINT2f pos) { char_position = pos; }
void SetChar_Velocity(VECTOR2f vel) { char_velocity = vel; }

to

void SetChar_Position(const POINT2f& pos) { char_position = pos; }
void SetChar_Velocity(const VECTOR2f& vel) { char_velocity = vel; }
Community
  • 1
  • 1
cmourglia
  • 2,423
  • 1
  • 17
  • 33