0

why am I getting error Genre genre; part. It says ‘Genre’ does not name a type Can somebody explain?

// The structure of the Song
typedef struct song
{
    int id_playlist;
    char *artist;
    Genre genre;
    double duration;
    char *name;
    struct song *next;
} Song;


// Enumeration for song genre
typedef enum 
{
    ROCK = 0,
    RAP,
    POP,
    METAL
} Genre;


Song *New_song(char *name, char *artist, double duration, Genre genre);
void Print_song(Song *song);
3
  • 1
    Hint: Declare it before you use it. Compilers can only work with what they've seen up to that point. They don't look into the future. Commented May 30, 2021 at 15:31
  • @tadman thank you, I forgot about that Commented May 30, 2021 at 15:40
  • 1
    Treat it like telling the compiler a story. You can't involve characters you haven't introduced yet, or it's going to ask "Wait, who's this new person?" Commented May 30, 2021 at 15:41

1 Answer 1

2

When the compiler reads the Genre genre; line in the structure definition, it hasn't interpreted yet the definition of Genre below. If you swap both, this should work fine:

// Enumeration for song genre
typedef enum 
{
    ROCK = 0,
    RAP,
    POP,
    METAL
} Genre;

// The structure of the Song
typedef struct song
{
    int id_playlist;
    char *artist;
    Genre genre;
    double duration;
    char *name;
    struct song *next;
} Song;

In other words, you have to define something before you can use it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.