为什么在此代码中引发错误?
对象属性具有以下值(通过方法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
是预处理程序指令。它在编译时发生,并不关心您的运行时活动(例如if()
语句)。
如果需要编译时错误,则需要使用编译时指令,例如#if
。
如果需要运行时错误,则需要使用类似throw
等的运行时错误抛出