C++ 此函数在成员函数中的使用无效
我的类中有一个方法如何返回对对象的引用,但我不知道如何在这个方法中访问我的attributeC++ 此函数在成员函数中的使用无效,c++,C++,我的类中有一个方法如何返回对对象的引用,但我不知道如何在这个方法中访问我的attribute Particule& update(double timestamp) { this->vx += timestamp; } vx是我的对象的属性Particule。但是如果我试图用这个访问vx,我会出错,为什么?我想这会管用的 我的班级定义: #include <stdio.h> #include <iostream> class Particule{
Particule& update(double timestamp)
{
this->vx += timestamp;
}
vx
是我的对象的属性Particule
。但是如果我试图用这个访问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';
}
};
#包括
#包括
类分词{
公众:
双rx,ry;//位置
双vx,vy;//速度
双fx,fy;//力
双质量;//质量
分词();
微粒(双rx、双ry、双vx、双vy、双fx、双fy、双质量);
微粒和更新(双时间戳);
friend std::ostream&operator如果定义在类之外,则必须执行以下操作:
Particule& Particule::update(double timestamp)
{
this->vx += timestamp;
return *this;
}
此代码是否在类定义中?错误为:在非静态成员函数之外使用“this”无效
请提供一个在其类定义之外声明的update
。将方法移动到类定义中或将其更改为“your\u class\u name”: