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

吃可爱长大的小学妹 提交于 2019-12-20 04:24:24

问题


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:


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.



来源:https://stackoverflow.com/questions/34267535/error-c2355-this-can-only-be-referenced-inside-non-static-member-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!