Xcode中应有表达式错误

Expected Expression Error in Xcode

本文关键字:表达式 错误 Xcode      更新时间:2023-10-16

我正在为一个游戏制作移动函数,但我得到了一个预期的表达式错误,我不知道为什么,我所做的似乎是合法的。

void Ant::move()
{
int dir=rand()%4;
if (dir==0)
{
    if ((y>0) && (world->getAt(x,y-1)==NULL))
    {
        world->setAt(x,y-1,world->getAt(x,y));
        world->setAt(x,y,NULL);
        y++;
    }
}
else
{
    if ((x<WORLDSIZE-1) && (world->getAt(x+1,y)==NULL))
    {
        world->setAt(x-1,y,world->getAt(x,y));
        world->setAt(x,y,NULL);
        x--;
    }
}
else
{
    if ((x<WORLDSIZE-1) && (world-getAt(x+1,y)==NULL))
    {
        world->setAt(x+1,y,world->getAt(x,y));
        world->setAt(x,y,NULL);
        x++;
    }
}
}

问题出在第二个电话上。

我认为问题是:

world-getAt(x+1,y)==NULL

您忘记了>

world->getAt(x+1,y)==NULL

在第二个if语句中。

在第一个else之后缺少if。你现在有

if {
    ...
} else { // here you need an if - or revise the structure
} else {
}

例如,试试。。。

void Ant::move()
{
    int dir=rand()%4;
    if (dir==0)
    {
        if ((y>0) && (world->getAt(x,y-1)==NULL))
        {
            world->setAt(x,y-1,world->getAt(x,y));
            world->setAt(x,y,NULL);
            y++;
        } else
        if ((x<WORLDSIZE-1) && (world->getAt(x+1,y)==NULL))
        {
            world->setAt(x-1,y,world->getAt(x,y));
            world->setAt(x,y,NULL);
            x--;
        } else
        if ((x<WORLDSIZE-1) && (world-getAt(x+1,y)==NULL))
        {
            world->setAt(x+1,y,world->getAt(x,y));
            world->setAt(x,y,NULL);
            x++;
        }
    }
}