错误:在if之前需要不合格的id

error: expected unqualified-id before if

本文关键字:不合格 id if 错误      更新时间:2023-10-16

我已经查看了以前的答案,但没有一个解释我为什么会出现这个错误。

这是我的错误代码。它发生在"if(pathID==2…)"和之后的每个if语句上。

void add_path(int a,int b, int current_step,int pathID){
    if(pathID == 0){
            path[current_step] = new step(a,b,"Filled A",path[current_step]);
    }
    if(pathID == 1)
            path[current_step] = new step(a,b,"Filled B",path[current_step]);
    }
    if(pathID == 2){
            path[current_step] = new step(a,b,"Empty A",path[current_step]);
    }
    if(pathID == 3){
            path[current_step] = new step(a,b,"Empty B",path[current_step]);
    }
    if(pathID == 4){
            path[current_step] = new step(a,b,"Pour B to A",path[current_step]);
    }
    if(pathID == 5){
            path[current_step] = new step(a,b,"Pour A to B",path[current_step]);
    }
}

所有这些代码所要做的就是将其添加到数组中给定位置的链表中。路径ID被传入,告诉它执行了什么操作,所以我们知道我们知道要向链表添加什么。

在程序的后期,我使用该链接列表来确定采取了哪些操作。我仍然需要使它成为一个双链接列表,这样它就不会反向打印,但这是另一个问题。

后忘记了大括号

if(pathID == 1)

加上它就会很好用。