c++: Can’t modify class object parameter

  accessor, c++, class

I recently created two classes in my c++ project, one is Player class and the other Vector class (not the array).

Player.h

#ifndef Player_h
#define Player_h

#include <iostream>
#include <string>
#include <cmath>

/* Player */
 class Player{
 public:
    /* Constructor */
    Player(std::string name = "name", double x = 0, double y = 0, double z = 0);
    Player(const Player& player);

    /* Methods */
    void position(double x, double y, double z);
    void velocity(double x, double y, double z);

    /* Read only */
    Vector position() const;
    Vector velocity() const;

    /* Operator */
    friend std::ostream& operator<<(std::ostream& stream, const Player& player);
 private:
    std::string m_name;
    Vector m_position;
    Vector m_velocity;
};

/* Constructor */
 Player::Player(std::string name, double x, double y, double z)
: m_name(name), m_position(x, y, z), m_velocity(0){}

 Player::Player(const Player& player)
: m_name(player.m_name), m_position(player.m_position), m_velocity(player.m_velocity){}

/* Methods */
 void Player::position(double x, double y, double z){
    m_position.x(x);
    m_position.y(y);
    m_position.z(z);
}

 void Player::velocity(double x, double y, double z){
    m_velocity.x(x);
    m_velocity.y(y);
    m_velocity.z(z);
}

/* Read only */
 Vector Player::position() const{ return m_position; }
 Vector Player::velocity() const{ return m_velocity; }

/* Operator */
 std::ostream& operator<<(std::ostream& stream, const Player& player){
    stream << player.m_name << " >> pos" << player.m_vector << "";
    return stream;
}
/* Player */

#endif /* Player_h */

Vector.h

#ifndef Vector_h
#define Vector_h

#include <iostream>
#include <string>
#include <cmath>

/* Vector */
 class Vector{
 public:
    /* Constructor */
    Vector(double x = 0, double y = 0, double z = 0);
    Vector(const Vector& vec);

    /* Methods */
    void x(double x);
    void y(double y);
    void z(double z);
    double norm();

    /* Read only */
    double x() const;
    double y() const;
    double z() const;
    bool isEqual(const Vector &vec) const;

    /* Operator & Cie */
    friend std::ostream& operator<<(std::ostream& stream, const Vector& vec);
    Vector& operator+=(const Vector& vec);
    Vector& operator-=(const Vector& vec);
    friend Vector operator+(const Vector& vecA, const Vector& vecB);
    friend Vector operator-(const Vector& vecA, const Vector& vecB);
    friend bool operator==(const Vector& vecA, const Vector& vecB);
    friend bool operator!=(const Vector& vecA, const Vector& vecB);
    friend bool operator<(const Vector& vecA, const Vector& vecB);
    friend bool operator>(const Vector& vecA, const Vector& vecB);
    friend bool operator<=(const Vector& vecA, const Vector& vecB);
    friend bool operator>=(const Vector& vecA, const Vector& vecB);
 private:
    double m_x;
    double m_y;
    double m_z;
};

/* Constructor */
 Vector::Vector(double x, double y, double z)
: m_x(x), m_y(y), m_z(z) {
    if(x != 0 && y == 0 && z == 0){
        m_y = x;
        m_z = x;
    }
}

Vector::Vector(const Vector& vec)
: m_x(vec.m_x), m_y(vec.m_y), m_z(vec.m_z) {}

/* Methods */
 void Vector::x(double x){ m_x = x; }
 void Vector::y(double y){ m_y = y; }
 void Vector::z(double z){ m_z = z; }

 double Vector::norm(){
    return sqrt(pow(m_x, 2) + pow(m_y, 2) + pow(m_z, 2));
}

/* Read only */
 double Vector::x() const{ return m_x; }
 double Vector::y() const{ return m_y; }
 double Vector::z() const{ return m_z; }

 bool Vector::isEqual(const Vector &vec) const{
    return m_x == vec.m_x && m_y == vec.m_y && m_z == vec.m_z;
}

/* Operator & Cie */
 std::ostream& operator<<(std::ostream& stream, const Vector& vec){
    stream << "(" << vec.m_x << "; " << vec.m_y << "; " << vec.m_z << ")";
    return stream;
}

 Vector& Vector::operator+=(const Vector& vec){
    m_x += vec.x();
    m_y += vec.y();
    m_z += vec.z();
    return *this;
}

 Vector& Vector::operator-=(const Vector& vec){
    m_x -= vec.x();
    m_y -= vec.y();
    m_z -= vec.z();
    return *this;
}

 Vector operator+(const Vector& vecA, const Vector& vecB){
    Vector result(vecA);
    result += vecB;
    return result;
}

 Vector operator-(const Vector& vecA, const Vector& vecB){
    Vector result(vecA);
    result -= vecB;
    return result;
}

 bool operator==(const Vector& vecA, const Vector& vecB){ return vecA.m_x == vecB.m_x && vecA.m_y  == vecB.m_y && vecA.m_z == vecB.m_z; }
 bool operator!=(const Vector& vecA, const Vector& vecB){ return !(vecA == vecB); }
 bool operator<(const Vector& vecA, const Vector& vecB){
    Vector copyA(vecA), copyB(vecB);
    return copyA.norm() < copyB.norm();
}
 bool operator>(const Vector& vecA, const Vector& vecB){ return !(vecA < vecB); }
 bool operator<=(const Vector& vecA, const Vector& vecB){ return vecA < vecB || vecA == vecB; }
 bool operator>=(const Vector& vecA, const Vector& vecB){ return vecA > vecB || vecA == vecB; }
/* Vector */

#endif /* Vector_h */

Main

#include <iostream>
#include <string>
#include <cmath>

#include "Vector.h"
#include "Player.h"

/* Main */
 int main(int argc, char* argv[]){
    Player player("Joueur 1");

    player.position(-10, 65, 876);
    player.position().x(5);

    std::cout << player.position() << std::endl;

    std::cin.get();
}
/* Main */

So, in my Vector class I created a method that permits to modify x, y and z values. (void x(double x); void y(double y); void z(double z);)
In my player class I create a method that permits to access player position (Vector). (Vector position() const;)
My question is why can’t I modify player x_position like that? :

player.position().x(5);

I think that I have to use pointer or address but I don’t know how.

Source: Windows Questions C++

LEAVE A COMMENT