i’m developing an app that uses Box2d. When I try to do something like this :
NSString *s = @"wood";
bodyDef.userData.name = s;
I get error
request for member 'name' in 'bodyDef.b2BodyDef::userData', which is of non-class type 'void*'
Thanks!
You cannot dereference a
void*pointer in C, C++, or Objective-C. Avoid*pointer is a pointer to an unknown data type — when you use one, you’re telling the compiler that “it points to something, but I have no idea what exactly that something is”.When you assign your user data to a Box2D object, it treats it as an opaque pointer: it copies it around as needed, but it never looks at what data the pointer points to. That’s your job.
In order to get a usable pointer back out, you just need to cast it (implicitly or explicitly) to the correct type. Assuming you’re using a common data structure, you can do something like this:
Note that you can only make an implicit cast from
void*to other pointer types in C and Objective-C — doing so is illegal in C++ and Objective-C++ and requires an explicit cast.