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.
精彩评论