I have a code that defines a simple enumerated type, then loop through it to print the corresponding syllable.
#include <stdio.h>
typedef enum syllable
{
Do=1, Re=2, Mi=3, Fa=4, So=5, La=6, Ti=7
} Syllable;
void Sound(Syllable sy)
{
switch (sy)
{
case Do:
puts("Do"); return;
case Re:
puts("Re"); return;
case Mi:
puts("Mi"); return;
case Fa:
puts("Fa"); return;
case So:
puts("So"); return;
case La:
puts("La"); return;
case Ti:
puts("Ti"); return;
}
puts("Sing together~");
}
int main(void)
{
Syllable tone;
for (tone=Do; tone<Ti; tone++)
Sound(tone);
return 0;
}
However, this code is raising error no 'operator++(int)' declared for postfix '++' [-fpermissive] on compilation at line tone++. What am I doing wrong here, and how can I fix it such that I can loop through Syllable correctly?
It seems you are compiling your code as a C++ code.
From the C++14 Standard (5.2.6 Increment and decrement)
and (3.9.1 Fundamental types)
and
As you can see enumerations are not included in the arithmetic types in C++. So the postfix increment operator for enumerations is not defined in C++.
On the other hand, in C (The C Standard (6.5.2.4 Postfix increment and decrement operators)
and (6.2.5 Types)
In C enumerations are included in the family of integer types and correspondingly in the family of real types for which the postfix increment operators is defined.
So compile your code as a C code.
In C++ you need explicitly to overload the postfix increment operator as a user-defined function something like