尽管if子句为假,为什么会引发错误?

问题描述 投票:0回答:2

为什么在此代码中引发错误?

对象属性具有以下值(通过方法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;
    }
  }
}
c++ c-preprocessor
2个回答
2
投票

#error是预处理程序指令。除非通过#if或类似的预处理程序指令将其消除,否则将始终处理该错误。

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


2
投票

#error是预处理程序指令。它在编译时发生,并不关心您的运行时活动(例如if()语句)。

如果需要编译时错误,则需要使用编译时指令,例如#if

如果需要运行时错误,则需要使用类似throw等的运行时错误抛出

© www.soinside.com 2019 - 2024. All rights reserved.