Trying to use std::list in objective-c program
I'm trying to use a std::list in my objective-c program 开发者_C百科and having run-time issues.
I have (edit for brevity)
#import <list>
...
@interface Foo {
std::list<Bar *> *myList;
}
...
@implementation Foo
-(void)loopList {
myList = new std::list<Bar *>;
for (std::list<Bar *>::iterator ii; ii != myList->end(); ++ii)
{
// Do nothing here
}
}
Everything compiles fine, but I get a EXC_BAD_ACCESS while executing the for loop. I'm not doing anything with the contents, just looping though them (for now).
I'd use NSMultableArray, but I need to quickly insert and remove elements while I'm iterating, which is not possible. Also, I'd like to use the stl for speed issues.
I have my files set to sourcecode.cpp.objcpp.
Thanks in advance.
This line:
for (std::list<Bar *>::iterator ii; ii != myList->end(); ++ii)
Needs to become:
for (std::list<Bar *>::iterator ii = myList->begin(); ii != myList->end(); ++ii)
In your version, ii
is never assigned a value, so it has nothing to iterate over. Based on the code you posted it's not clear why, but because of this you later attempt to dereference an invalid memory address and that's why you generate an exception (EXC_BAD_ACCESS).
I don't know about objective C, but in C++ you need to tell the iterator which list instance it is iterating over:
for (std::list<Bar *>::iterator ii = myList->begin();
ii != myList->end(); ++ii)
精彩评论