嵌套结构定义

发布于 2024-11-05 01:36:45 字数 865 浏览 2 评论 0原文

我正在处理计算机体系结构课程的模拟。我尝试使用 gcc v4.5.2 来编译 MSYS1.1/MinGW 包。

Makelog:

    In file included from
    sim-outorder.c:107:0: bpred.h:214:5:
    error: expected specifier-qualifier-list before 'uint'
    ... 
    make: *** [sim-outorder.o] Error 1

我检查了bpred.h中的第214行,有一个嵌套结构定义为:

208 /* branch predictor update information */ 
209 struct bpred_update { 
210 char *pdir1;         /* direction-1 predictor counter */ 
211 char *pdir2;         /* direction-2 predictor counter */ 
212 char *pmeta;  /* meta predictor counter */ 
213 struct dir{             /* predicted directions */ 
214  uint bimod  : 1;    /* bimodal predictor */ 
215  uint twolev : 1;    /* 2-level predictor */ 
216  uint meta   : 1;    /* meta predictor (0..bimod / 1..2lev) */ 
217 } dir; 
218 };

这个结构块有什么问题? 你能帮忙吗? 谢谢!

I'm dealing with sim for computer architecture course. I try to compile package with MSYS1.1/MinGW with gcc v4.5.2.

Makelog:

    In file included from
    sim-outorder.c:107:0: bpred.h:214:5:
    error: expected specifier-qualifier-list before 'uint'
    ... 
    make: *** [sim-outorder.o] Error 1

I checked the line 214 in bpred.h, there is a nested struct definition as:

208 /* branch predictor update information */ 
209 struct bpred_update { 
210 char *pdir1;         /* direction-1 predictor counter */ 
211 char *pdir2;         /* direction-2 predictor counter */ 
212 char *pmeta;  /* meta predictor counter */ 
213 struct dir{             /* predicted directions */ 
214  uint bimod  : 1;    /* bimodal predictor */ 
215  uint twolev : 1;    /* 2-level predictor */ 
216  uint meta   : 1;    /* meta predictor (0..bimod / 1..2lev) */ 
217 } dir; 
218 };

What's wrong with this struct block?
Could you please help??
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

离笑几人歌 2024-11-12 01:36:45

我相信您被禁止命名内部结构:

208 /* branch predictor update information */ 
209 struct bpred_update { 
210 char *pdir1;         /* direction-1 predictor counter */ 
211 char *pdir2;         /* direction-2 predictor counter */ 
212 char *pmeta;  /* meta predictor counter */ 
213 struct     {             /* predicted directions */  
/******    ^^^^ --- NO NAME HERE -- THIS IS WHAT YOU MUST CHANGE */
214  uint bimod  : 1;    /* bimodal predictor */ 
215  uint twolev : 1;    /* 2-level predictor */ 
216  uint meta   : 1;    /* meta predictor (0..bimod / 1..2lev) */ 
217 } dir; 
218 };

我怀疑这是因为“说明符限定符列表”听起来像一个令牌,它是诸如 struct dim = {0}; 之类的东西的一部分。

I believe that you are disallowed from naming the inner struct:

208 /* branch predictor update information */ 
209 struct bpred_update { 
210 char *pdir1;         /* direction-1 predictor counter */ 
211 char *pdir2;         /* direction-2 predictor counter */ 
212 char *pmeta;  /* meta predictor counter */ 
213 struct     {             /* predicted directions */  
/******    ^^^^ --- NO NAME HERE -- THIS IS WHAT YOU MUST CHANGE */
214  uint bimod  : 1;    /* bimodal predictor */ 
215  uint twolev : 1;    /* 2-level predictor */ 
216  uint meta   : 1;    /* meta predictor (0..bimod / 1..2lev) */ 
217 } dir; 
218 };

I suspect this because a "specifier-qualifier-list" sounds like a token which is part of something like struct dim = {0};.

素手挽清风 2024-11-12 01:36:45

在 C 中,内部结构不允许有名称,这是由于您通常需要如何在 C 中引用结构:

struct A {
   ...
};

struct B {
    struct A my_a;
};

因此,天真的 C 编译器不清楚它是否应该期待变量或类型声明。

然而,在这种情况下,使用 C++ 编译器它会像您期望的那样工作,因为 C++ 不期望变量减速的结构前缀。

解决方案是从内部结构中删除该名称,或者在另一个结构之外声明它。

In C, inner structs are not allowed to have names, this is due to how you need to reference structures in C normally:

struct A {
   ...
};

struct B {
    struct A my_a;
};

It is thus unclear to a naive C compiler if it is should be expecting a variable, or a type declaration.

This is a case where however, using a c++ compiler it would work as you expect it to, due to the fact C++ does not expect the struct prefix for variable decelerations.

The solution is to either remove the name from the inner struct, or declare it outside of the other struct.

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