"[Error] expected unqualified-Id before 'if'"这个代码块是什么意思

what is "[Error] expected unqualified-Id before 'if'" mean in this block of code

本文关键字:代码 是什么 意思 if before unqualified-Id Error expected      更新时间:2023-10-16

我试图衡量作为学校项目的示例,我真的不知道我做错了什么,当然会在功能中添加IF语句,因为它可以使用't呼叫自己

    #include <iostream>
    using namespace std;
    int main(){
 struct worker{
    int salary;
    float taxperc;
    int bills;
    int home_needs;
    int bonus;
    bool measure;
    void saving(){
        float tax = (salary/100)*taxperc;
        cout << salary - tax -bills-home_needs  << endl;
    }
    if (measure)
        saving();
};
worker me = {50000, 2.5, 150, 3000, 0, true};
}

不确定您要实现的目标,但是您不能在struct范围级别上具有if。直接在struct { ... }中,您只能拥有:

  • 成员值(字段)
  • 成员功能

而不是常规代码,语句等。您实际上期望执行此类代码?

您的选择是:

  • if (measure) { saving(); }放入另一个成员函数
  • 从结构外部的main功能中调用saving(),例如me.saving()

P.S。我强烈建议您也为您的代码缩进,为了可读性。