c++ - 错误 C2355 : 'this' : can only be referenced inside non-static member functions or non-static data member initializers

标签 c++ pointers 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;

有什么想法吗?

那是固定的。

现在这部分代码出现同样的错误...

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

它还说

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

最佳答案

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

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

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

关于c++ - 错误 C2355 : 'this' : can only be referenced inside non-static member functions or non-static data member initializers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34267535/

相关文章:

C++ 我是否正确添加和访问我的 vector ?

c++ - LookUp Table for 16-Bit Mat 高效的方法?

c++ - 编译器如何理解指针类型?

c++ - 指针上的运算符<(小于)是否一致?

c++ - 在解析器中提升 Spirit 段错误

c++ - C++ 中用户定义的字符串转换(类似于 Python 中的 __repr__)

C++ 多重继承 + 虚函数(- 歧义)= 奇怪的行为(也是函数指针)

Java:替代传递 "this"作为构造函数参数以引用创建对象

javascript - 这。引用对象而不是窗口对象

javascript - JSLint 不喜欢 .bind(this),但没有它,就无法访​​问 this.obj