不断收到错误:“redefinition of typedef ‘MYSTRUCT’”和“之前声明的‘MYSTRUCT’;在这里”
我不断收到一个错误,如果我没有在定义之前预先声明使用的结构,那么应该会发生错误,但我做到了!!:“typedef struct Campeonato Campeonato; typedef struct jogo jogo ;” (如下面的代码所示)。谁能告诉我为什么我会收到以下错误以及我的代码出了什么问题:
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;
i keep getting the an error that was supposed to happen if i haven't had pre-declared the structures used before its definition, but i did!!: "typedef struct campeonato campeonato; typedef struct jogo jogo;" (as shown in the code below). Can anyone tell me why I'm getting the following errors and what's wrong with my code:
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
and the piece of code that generates those errors is...
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是前向声明。这是一个 typedef。前向声明很简单:
就像现在一样,您键入 def Campeonato 和 jogo 两次,因此会出现错误。
请注意,使用前向声明将允许您在定义结构之前使用它们,您仍然必须在
time
和struct 中将它们用作
。struct Campeonato
campeonato
中的 jogoIs not a forward declaration. It's a typedef. A forward declaration would simply be:
As it is now, you typedef campeonato and jogo twice, hence the error.
Note that using the forward declaration will allow you to use the structs before they are defined, you'd still have to use them as
struct campeonato
intime
andstruct jogo
incampeonato
.