尽管 if 子句为 false,但为什么会抛出错误?

Why is the error thrown although the if-clause is false?

为什么会在这段代码中抛出错误?

对象属性具有以下值(通过方法 addGroup() 输出):

####### ADDGROUP ######                                                                                                                                                                            
isBasic: 0                                                                                                                                                                                         
types[0]: Z                                                                                                                                                                                        
####### ADDGROUP ###### 

因此,由于 isBasic 为假且 types[0] = Z(如上面的输出所示),根据我的理解,不应抛出错误:

void addGroup(int newGroup) {
    cout<<"####### ADDGROUP ######"<<endl;
    cout<<"isBasic: ";
    cout<<isBasic<<endl;

    cout<<"types[0]: ";
    cout<<types[0]<<endl;
    cout<<"####### ADDGROUP ######"<<endl;

  if(isBasic == true && types[0] != 'Z') {
    #error "Sensor defined as basicComponent, type(s) already set. Basic component is either group- OR type-specific but never both!"
  }

  for (int i=0; i < 3; i++){
    if(groups[i] == 99) {
      groups[i] = newGroup;
    break;
    }
  }
}

#error 是预处理器指令。该错误将始终得到处理,除非它被 #if 或类似的预处理器指令删除。

预处理发生在编译之前。运行时条件不能影响它。

#error 是预处理器指令。它发生在编译时,并不关心你的运行时activity(比如你的if()语句)。

如果你想要一个编译时错误,那么你需要使用像#if这样的编译时指令。

如果你想要一个运行时错误,那么你需要像 throw 等人那样使用运行时错误抛出。