错误 C2355:'this':只能在非静态成员函数或非静态数据成员初始值设定项中引用

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

本文关键字:引用 数据成员 静态成员 this C2355 函数 错误 静态      更新时间:2023-10-16

我在编译代码时遇到了一些问题。上面写着,

错误C2355:"this":只能在非静态成员内部引用函数或非静态数据成员初始化程序

错误显示在上的代码部分

    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;

有什么想法吗?

这个问题已经解决了。

现在这部分代码出现相同错误。。。

    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"的左侧必须指向类/结构/联合/泛型类型

您需要将类作用域添加到方法中,例如,如果您的类名为YourClass,那么您的函数将是

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

否则,getR是一个自由函数,因此没有this可操作。其他方法也是如此。