我在编译代码时遇到了一些问题。它说,
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/