gpt4 book ai didi

c - 不断收到错误 : "redefinition of typedef ' MYSTRUCT '"and "previous declaration of 'MYSTRUCT' was here"

转载 作者:行者123 更新时间:2023-11-30 14:27:35 24 4
gpt4 key购买 nike

我不断收到一个错误,如果我没有在定义之前预先声明使用的结构,那么应该会发生错误,但我做到了!!:“typedef struct Campeonato Campeonato; typedef结构乔戈乔戈;” (如下面的代码所示)。谁能告诉我为什么会出现以下错误以及我的代码有什么问题:

c:21: error: redefinition of typedef 'campeonato'
c:5: error: previous declaration of 'campeonato' was here
c:29: error: redefinition of typedef 'jogo'
c:6: error: previous declaration of 'jogo' was here

生成这些错误的代码是......

typedef struct campeonato campeonato;
typedef struct jogo jogo;

typedef struct time{
char nome[32];
//existe uma correspondencia entre jogos[i][] e campeonatos[i]
jogo *jogosDeCadaCampeonato;
campeonato *campeonatos[];
}time;

typedef struct campeonato{
char nome [100];
int nro_participantes;
int nro_jogos;
time *times;
jogo *jogos;
}campeonato;

typedef struct jogo{
time* timeA;
time* timeB;
time* vencedor;
int golsA;
int golsB;
}jogo;

最佳答案

typedef struct campeonato campeonato;
typedef struct jogo jogo;

不是前向声明。这是一个 typedef。前向声明就是:

struct campeonato;
struct jogo;

就像现在一样,您输入 def Campeonato 和 jogo 两次,因此出现错误。

请注意,使用前向声明将允许您在定义结构之前使用它们,您仍然必须将它们用作 struct campeonatotimestruct jogocampeonato .

关于c - 不断收到错误 : "redefinition of typedef ' MYSTRUCT '"and "previous declaration of 'MYSTRUCT' was here",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7772500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com