Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

pointers

this

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

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

part of the code where the error shows up

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

also here

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

Any ideas?

THAT WAS FIXED.

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;

it also says

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

like image 597
user3083761 Avatar asked Dec 11 '22 19:12

user3083761


1 Answers

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

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

like image 132
Cory Kramer Avatar answered Dec 13 '22 10:12

Cory Kramer