不断收到错误:“redefinition of typedef ‘MYSTRUCT’”和“之前声明的‘MYSTRUCT’;在这里”

发布于 2024-12-10 03:12:40 字数 1011 浏览 1 评论 0原文

我不断收到一个错误,如果我没有在定义之前预先声明使用的结构,那么应该会发生错误,但我做到了!!:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征棹 2024-12-17 03:12:40
typedef struct campeonato campeonato;
typedef struct jogo jogo;

不是前向声明。这是一个 typedef。前向声明很简单:

struct campeonato;
struct jogo;

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

请注意,使用前向声明将允许您在定义结构之前使用它们,您仍然必须在 timestruct 中将它们用作 struct Campeonato campeonato 中的 jogo

typedef struct campeonato campeonato;
typedef struct jogo jogo;

Is not a forward declaration. It's a typedef. A forward declaration would simply be:

struct campeonato;
struct jogo;

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 in time and struct jogo in campeonato.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文