错误C2355:"this":只能在非静态成员函数或非静态数据成员初始化器中引用 [英] error C2355: 'this' : can only be referenced inside non-static member functions or non-static data member initializers

查看:2095
本文介绍了错误C2355:"this":只能在非静态成员函数或非静态数据成员初始化器中引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于错误C2355:"this":只能在非静态成员函数或非静态数据成员初始化器中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆