开发者

error C2440: '=' : cannot convert from 'bool' to 'bool *'

开发者 https://www.devze.com 2023-01-01 07:57 出处:网络
I\'m getting said error on this line \"b = true\". Now Why am I getting this error? Aren\'t I pointing to TurnMeOn and thus saying TurnMeOn = true?

I'm getting said error on this line "b = true". Now Why am I getting this error? Aren't I pointing to TurnMeOn and thus saying TurnMeOn = true?

class B{
void turnOn(bool *b){b = true}
};

int main(){
B *b = new B();
bool turnMeOn = false;
b->turnOn(&turnMeOn);
cout <<开发者_如何学C; "b = " << turnMeOn << endl;
}


b->turnOn(&turnMeOn);

and

   *b = true;


turnOn requires a pointer to bool as parameter. You're using it as an actual bool. I guess you're looking for a reference, i.e. bool& b as parameter declaration in your method.


No. As you've written it, it would need to be *b = true.

Alternatively, you could write the function to take a reference to a bool, so that

void turnOn(bool &b) { b = true; }

would be correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消