I have a lot of shared C++ code that I’d like to use in my iPhone app. I added the .cpp and .h files to my Xcode project and used the classes in my Objective-C++ code. The project compiles fine with 0 errors or warnings.
However, when I run it in the simulator I get the following error when I attempt to access an STL method in my objective-c code (such as .c_str()):
Program received signal: “EXC_BAD_ACCESS”.
Unable to disassemble std::string::c_str.
Here’s an example of the code that causes the error:
[name setText:[[NSString alloc] initWithCString:myCPlusPlusObject->cppname.c_str()]];
where name is an NSTextField object and cppname is a std::string member of myCPlusPlusObject.
Am I going about this the right way? Is there a better way to use STL-laden C++ classes in Objective-C++? I would like to keep the common C++ files untouched if possible, to avoid having to maintain the code in two places.
Thanks in advance!
Make sure the string isn’t empty before passing it to the
initWithCStringfunction.Also the function you’re using has been deprecated, use this one instead.