C++:错误 C2675:一元"++"

C++ : error C2675: unary '++'

本文关键字:一元 C2675 错误 C++      更新时间:2023-10-16

我使用C 时有一个C2675错误。

#include<stdio.h>
int main(void){
   enum months {jan=1, feb, mar, apr, jun, aug, sep, oct, nov, dec};
   enum TV { kbs1 =9, kbs2 = 7, mbc =11, sbs = 6};
   enum months mon;
   printf("kbs1 : %d",kbs1);
   printf("kbs2 : %d",kbs2);
   printf("mbc : %d", mbc);
   printf("sbs : %d", sbs);
   for(mon = jan; mon<=dec; ++mon){
       printf("%d",mon);}
   return 0;

但是,有结果...

error C2675: unary '++' : 'main::months' does not define this operator or a conversion to a type acceptable to the predefined operator.

请帮助我如何解决此问题... T_T

一个(丑)解决方案是:

for( mon = jan; mon <= dec; mon = (months)(mon + 1) )
{
    printf("%d", mon);
}

,但它仅是因为您的枚举值是连续的,并且与您的TV enum无法使用。