错误C2355:"this":只能在非静态成员函数或非静态数据成员初始化器中引用

错误C2355:

问题描述:

我在编译代码时遇到一些问题.它说,

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

错误C2355:"this":只能在非静态成员中引用 函数或非静态数据成员初始化器

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

出现错误的部分代码

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

也在这里

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

有什么想法吗?

已修复.

现在这部分代码有相同的错误...

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;

它也说

错误C2227:'-> r'的左侧必须指向类/结构/联合/泛型类型

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

您需要将类范围添加到您的方法中,例如,如果您的类名为YourClass,则您的函数应为

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;
}

否则,getR是自由功能,因此没有this可以进行操作.其他方法也一样.

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