C枚举计划年度未编制的月份

问题描述 投票:1回答:1

我从我的教科书中得到了下面的代码示例,它仍然没有编译。

我在printf上写了Unary '++': 'months; does not define this operator or a conversion to a type acceptable to the predefined operator的错误。

我不知道该怎么办,因为这是我第一次尝试枚举,这实际上就是本书中的示例代码。它出什么问题了?

// Fig. 10.18: fig10_18.c
// Using an enumeration

#include <stdio.h>

// enumeration constants represent months of the year            
enum months {                                                    
    JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
}; 

int main(void) { 
    // initialize array of pointers
    const char *monthName[] = { "", "January", "February", "March", 
      "April", "May", "June", "July", "August", "September", "October",
      "November", "December" 
    };

    // loop through months
    for (enum months month = JAN; month <= DEC; ++month) {
        printf("%2d%11s\n", month, monthName[month]);
    } 
}
c enumeration
1个回答
0
投票

你所做的是在C中是正确的但在C ++中是不正确的。

您必须使用C编译器而不是C ++编译器。

您可以尝试使用-x c命令行选项告诉C ++编译器将源文件编译为C代码。

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