在成员函数中对此的使用无效

Invalid use of this in member function

本文关键字:无效 成员 函数      更新时间:2023-10-16

我的类中有一个方法how返回对对象的引用,但我不知道如何在这个方法中访问我的attribut。

Particule& update(double timestamp)
{
    this->vx += timestamp;
}

vx是我的对象Particule的属性。但是,如果我尝试用this访问vx,我会出错,为什么?我以为这会奏效。

我的类定义:

#include <stdio.h>
#include <iostream>
class Particule{
    public:
    double rx, ry;      //position
    double vx, vy;      //velocity
    double fx, fy;      //force
    double mass;        //mass
    Particule ();
    Particule(double rx, double ry, double vx, double vy, double fx, double fy, double mass);
    Particule& update(double timestamp);
    friend std::ostream& operator<<(std::ostream& str, Particule const& p)
    {
        return str <<
        "rx : " << p.rx <<
        " ry : " << p.ry <<
        " vx : " << p.vx <<
        " vy : " << p.vy <<
        " mass : " << p.mass << 'n';
    }
};

我不知道如何在我的方法更新中访问我的对象。我用object.update(timestamp); 调用此方法

如果定义在类之外,则必须执行以下操作:

Particule& Particule::update(double timestamp)
{
    this->vx += timestamp;
    return *this;
}