typedef ignored

At the line 10: [warning] 'typedef' was ignored in this declaration [enabled by default].
Why is the typedef ignored???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
typedef enum giorni{
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
};
int n;
giorni g;
int main(){
	printf("Inserisci un numero da 1 a 7: ");
	scanf("%d",&n);
	g=giorni(n);
	printf("\nGiorno corrispondente: %d",g);
}
the typedef keyword is ignored while compiling, the enum itself should not be ignored
how can i solve that?
Line 10: You didn't give the typedef a name.

I'm assuming from the code, this is C and not C++. You must give the typedef a name.
C won't recognize the use of the enum tag (giorni).

1
2
3
4
5
6
7
8
9
10
11
typedef enum giorni{
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
} days_of_week;     // This is the typedef name
...
days_of_week g;



Why doesn't it work?

#include<stdio.h>
typedef enum giorni{
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
}giorni_settimana;
int n;
giorni_settimana g;
int main(){
printf("Inserisci un numero da 1 a 7: ");
scanf("%d",&n);
g=giorni_settimana(n);
printf("\nGiorno corrispondente: %d",g);
}
What do you mean by "doesn't work"?

Do you get:
1. Compilation error
2. Linker error
3. Runtime crash
4. Unexpected result

Please, describe the error.
closed account (E0p9LyTq)
At the line 10: [warning] 'typedef' was ignored in this declaration [enabled by default].
Why is the typedef ignored???


Are you compiling this as C or C++?

If you are compiling this as a C++ program remove the typedef from the front of your enum declaration. The warning is you received is not an error, the code compiles properly as C++.
I removed the typedef but it didn't go, so i tried to modify something but at the line 15 it says: 'giorni_settimana' cannot be used as a function

#include<stdio.h>
enum giorni{
Lunedi,
Martedi,
Mercoledi,
Giovedi,
Venerdi,
Sabato,
Domenica
}giorni_settimana;
int n,g;
int main(){
printf("Inserisci un numero da 1 a 7: ");
scanf("%d",&n);
g=giorni_settimana(n);
printf("\nGiorno corrispondente: %d",g);
}
Why are you trying to call your enum instance as if it were a function? What are you trying to do?
I don't know how to call the enum!!!
closed account (E0p9LyTq)
I don't know how to call the enum!!!


an enum is not a function, it is a type similar to int or char.

An enum is another data type.

http://www.cplusplus.com/doc/tutorial/other_data_types/

In your first code example remove the typedef from before the enum and compile as C++. The warning is simply a warning that with C++ putting typedef before enum is not required. typedef enum is C code, enum by itself is C++ code.
Why are you trying to call your enum instance as if it were a function?

It did not look like a regular function call, when there was a typedef.
1
2
3
4
5
typedef T foo; // foo is a type. Same as T.
foo obj1 = foo( 42 ); // typecast by calling T's constructor that can accept int

T bar; // bar is an object
T obj2 = bar( 42 ); // bar is a "functor":  T obj2 = bar.operator()( 42 ); 



The enums are names for constant values. Writing:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

#define foo 7
enum { bar = 3 };
constexpr int gaz = 42;

int main() {
  std::cout << foo << ' ' << bar << ' ' << gaz << '\n';
  return 0;
}

is almost (but not quite) like writing:
1
2
3
4
5
6
#include <iostream>

int main() {
  std::cout << 7 << ' ' << 3 << ' ' << 42 << '\n';
  return 0;
}

Topic archived. No new replies allowed.