You can only use std::string inside a function, not as a class instance variable. C++ objects must* be accessed through pointers to the heap to be member variables of an ObjC class.
The only way around this is to create a large C++ class with all the member variables inside it, and allocate that one class on the heap.
You seem to have some aversion to allocating std::string on the heap. Not sure why, but you'll continue to hit your head getting C++ and ObjC to interact if you do so.
To summarise:
Good:
Code:
-(void)foo
{
std::string s;
// magic/your code here
// s is cleaned up when scope exits
}
Bad:
Code:
@interface bar {
std::string s; // compiler error here, std::string has virtual methods; cannot be a member variable
std::string* ps; // ok, by using a pointer ObjC doesn't have to worry about calling constructors, destructors, setting up vtables, etc, you will do that yourself manually with new and delete.
}
-(void)foo;
@end
*As noted below, very simple C++ classes can live inside ObjC classes as member variables if they have default constructors and no virtual functions.
-Objective-C classes cannot have instance variables of C++ classes that do not have a default constructor or that have one or more virtual methods, but pointers to C++ objects can be used as instance variables without restriction (allocate them with new in the -init method).
-C++ "by value" semantics cannot be applied to Objective-C objects, which are only accessible through pointers.
-An Objective-C declaration cannot be within a C++ template declaration and vice versa. However, Objective-C types, (e.g., Classname *) can be used as C++ template parameters.
-Objective-C and C++ exception handling is distinct; the handlers of each cannot handle exceptions of the other type.
-Care must be taken since the destructor calling conventions of Objective-C and C++s exception run-time models do not match (i.e., a C++ destructor will not be called when an Objective-C exception exits the C++ objects scope). The new 64-bit runtime resolves this by introducing interoperability with C++ exceptions in this sense