William
William

Reputation: 8808

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

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 << "b = " << turnMeOn << endl;
}

Upvotes: 1

Views: 10520

Answers (3)

Pieter
Pieter

Reputation: 2912

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.

Upvotes: 5

Anton
Anton

Reputation: 2693

b->turnOn(&turnMeOn);

and

   *b = true;

Upvotes: 8

Mark Rushakoff
Mark Rushakoff

Reputation: 258228

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.

Upvotes: 4

Related Questions