iphone - 遍历指向指针 vector 的指针

标签 iphone c++ cocos2d-iphone box2d

我正在尝试使用 cocos2d 和 Box2D 编写 iPhone 游戏,这意味着我必须混合使用 Objective-C 和 C++。我有一个关于使用指向我创建的 gameLevels 和 levelObjects 对象的指针的问题。

我有一个名为 GameLevel 的类,其中包含一个 levelObjects vector 。我认为使用指针是做到这一点的方法,以确保我使用尽可能少的内存。这意味着我的 vector 是

std::vector<LevelObject*>* levelObjects;

LevelObject 类包含 Sprite 、b2bodyDef 等,以及所有指针。

当我想绘制一个关卡时,当我想遍历这个 vector 时,问题就开始了。我写的代码是这样的:

-(void)startLevel:(b2World*) world withLevel:(GameLevel*) level{
std::vector<LevelObject*>::iterator it;
for (it = level->getLevelObjects()->begin() ; it != level->getLevelObjects()->end(); ++it) {
    CCSprite* sprite = it->sprite; //Here XCode complaints about "Member reference base type 'LevelObject *' is not a structure or union" and "Use of undeclared identifier 'sprite'". I cannot seem to access any of the objects variables/methods from it->

所以我要问的问题是:使用指向指针 vector 的指针,这是解决这个问题的好方法吗?为什么迭代不起作用?

谢谢:)

编辑:getLevelObjects() 是:

std::vector<LevelObject*>* getLevelObjects(){return levelObjects;}

最佳答案

您的迭代器指向指针,因此您需要两次取消引用:

(*it)->sprite

迭代器的取消引用,

*it

给你一个LevelObject*

关于iphone - 遍历指向指针 vector 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10580805/

相关文章:

iphone - 当另一个 Sprite 移动时如何检查 Sprite 的边界? (Cocos2D)

iphone - 为什么 CCLabelTTF 不在 @"\n"上开始新行?

ios - 由于内容中的换行导致 xml 解析不准确

ios - TouchesBegan 未调用

加载新 Controller 时,iPhone 横向模式切换为纵向模式

c++ - 错误 : An exception (first chance) at 0x76f6f9d2 in Boost_Mutex. exe: 0xC0000008: 指定了无效的句柄

c++ - 为什么我们说对象超出范围时调用析构函数?

c++ - lua_pop 与 lua_remove

iOS UI 没有显示它应该在的位置

ios - 让应用程序屏幕的大部分区域都覆盖着黑色是否可以节省电池电量?