【问题标题】:Problem in struct when creating structure c创建结构c时结构中的问题
【发布时间】:2021-08-18 02:51:36
【问题描述】:

为什么我收到错误 Genre genre; 部分。它说‘Genre’ does not name a type 谁能解释一下?

// 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);

【问题讨论】:

  • 提示:你使用它之前声明它。编译器只能使用他们到目前为止所看到的内容。他们不展望未来。
  • @tadman 谢谢,我忘了
  • 像给编译器讲故事一样对待它。你不能让你还没有介绍过的角色参与进来,否则它会问“等等,这个新人是谁?”

标签: c struct enums structure typedef


【解决方案1】:

当编译器读取结构定义中的Genre genre; 行时,它还没有解释下面Genre 的定义。如果你交换两者,这应该可以正常工作:

// 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;

换句话说,你必须先定义一些东西才能使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    相关资源
    最近更新 更多