Search code examples
c++box2dsfml

SFML and Box2D SetUserData can't convert argument from const char [4] to void *


I am coding a platformer in sfml and box2d, I want to code some boxes I can push around, but for some reason body->SetUserData("box") says that it cannot convert const char [4] to void *. I am following a tutorial. In the tutorial everything works fine, but for me this error appears:

Error C2664 'void b2Body::SetUserData(void *)': cannot convert argument 1 from 'const char [4]' to 'void *'

Tutorial: (It is in Russian) https://www.youtube.com/watch?v=-eJXg2tGcyM

Code for the box:

b2PolygonShape shape;
shape.SetAsBox(30 / SCALE, 30 / SCALE);

b2BodyDef bdef;
bdef.type = b2_dynamicBody;

bdef.position.Set(600 / SCALE, 100 / SCALE);
b2Body *body = World.CreateBody(&bdef);
body->CreateFixture(&shape, 2);
body->SetUserData("box"); 

Solution

  • That tutorial probably relied on non-conformant behavior in VS that allowed conversion of string literals to non-const-qualified pointer to char. So you should either modify your code to be const-correct (recommended) or enable that old behavior by using /Zc:strictStrings- compilation option (not recommended).