Android Studio "for each" Problem

Android Studio "for each" Problem
0

I made a game for Windows with Visual Studio. Everything works fine. And than i decided to make same game for android. But i have an error on Android Studio. I used “for each” in one class. It was work on visual studio but android studio gives error.

Code :

    void HelloWorld::update(float dt)
{
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    cocos2d::Vector<Node*> children = this->getChildren();
    for each ( Node * child in children)
    {
        BuildingSprite* spriteBuilding = (BuildingSprite*)child;
        if(spriteBuilding->getTag() == BUILDING_ID_TAG)
         {
          spriteBuilding->setPositionX(spriteBuilding->getPositionX() - BUILDING_MOVEMENT_SPEED);
      if (spriteBuilding->getPositionY() < - spriteBuilding->getContentSize().width)
       {
        this->removeChild(spriteBuilding);
        BuildingPool::getBuildings()->returnBuilding(spriteBuilding);
       }
     }
   }
}

Error:
D:\CocosProjects\SteamFlyDemo\Classes\HelloWorldScene.cpp:63:9: error: expected '(' after 'for'

Maybe this works:

for (auto child : children)
{

}

3 Likes

There is no each.

1 Like

Dr Google would have helped: for each

This non-standard keyword is available in both C++/CLI and native C++ projects. However, its use is not recommended. Consider using a standard Range-based for Statement (C++) instead.

That’s the reason it won’t work outside the Microsoft ecosystem.

4 Likes

So how can i solve this problem? Is there anything else I can use instead of “foreach”? Or do I have the chance to make the android version of my game on visual studio or visual studio code?

You don’t need to do anything except use the correct syntax and it will work in both VS and AS.

for loops are part of the c++ spec.

Spending some time brushing up on C++11 would help, but aside from that, did you actually click any of the links in my previous post? You would have eventually found yourself here: Range-based for Statement (C++). The solution is literally there, which is exactly what everyone who’s replied to this thread has pointed out to you.

More info here: range-for

1 Like

Thanks for your advice. I solved the problem :wink:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.