error C2355: 'this' : can only be referenc

2020-04-16 08:26发布

I'm having some problems compiling my code. It says,

error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers

part of the code where the error shows up

    double getR() {
    return this->r;
}
double getG() {
    return this->g;
}
double getB2() {
    return this->b2;
}

also here

    rez.r = this->r / 2 + a.getR() / 2;
    rez.g = this->g / 2 + a.getG() / 2;
    rez.b2 = this->b2 / 2 + a.getB2() / 2;

Any ideas?

THAT WAS FIXED.

Same error on this part of code now...

    rez.r = this->r / 2 + a.getR() / 2;
    rez.g = this->g / 2 + a.getG() / 2;
    rez.b2 = this->b2 / 2 + a.getB2() / 2;

it also says

error C2227: left of '->r' must point to class/struct/union/generic type

1条回答
2楼-- · 2020-04-16 09:20

You need to add the class scope to your methods, for example if your class is named YourClass then your function would be

double YourClass::getR() {
    return this->r;
}

Otherwise getR is a free function, and therefore has no this to operate on. The same goes for your other methods.

查看更多
登录 后发表回答