【问题标题】:C error expected identifierC 错误预期标识符
【发布时间】:2012-11-15 22:42:37
【问题描述】:

到目前为止,我一直在尝试测试我的代码,但在编译测试运行时出现错误。

这是我的代码:

mips_op.h 文件

#ifndef MIPS_OP_H
#define MIPS_OP_H

typedef enum {
    R, I, J
} op_type;

typedef struct op_instr {
    op_type op_t; // instruction type {R, I, J}
    int opcode : 6; // instruction opcode - 6-bit integer

    // if the instruction type is J
    #if op_t == J

    int address : 26; // address to jump to - 26-bit integer

    #else // if the instruction type is R or I

    int rs : 5; // the output - 5-bit integer
    int rt : 5; // the first operand - 5-bit integer

        #if op_t == R // if instruction type is R

        int rd : 5; // the second operand - 5-bit integer
        int shamt : 5; // the shift amount field - 5-bit integer
        int funct : 6; // the function field

        #endif

        #if op_t == I // if instruction type is R

        int immediate : 16; // the immediate field - 16-bit integer

        #endif

    #endif
};

#endif

这里是 main.c 文件

#include <stdio.h>
#include "mips_op.h"

int main (void) {
    printf("Before instr\n");

    op_instr add;

    printf("After instr\n");

    return 0;
}

这是我遇到的错误

In file included from main.c:2:0:
mips_op.h:9:10: error: expected ')' before 'op_t'
main.c: In function 'main':
main.c:7:2: error: unknown type name 'op_instr'

我的代码有什么问题?为什么会出现此错误?

谢谢

编辑:将括号固定为大括号

【问题讨论】:

  • 您似乎误以为可以使用预处理器条件在运行时动态定义结构?考虑为这种事情使用联合。
  • 哦,好的,非常感谢保罗
  • 阅读错误信息可能会有所帮助。任何时候你看到一个 ')' ,你就可以确信在某个地方有一个未封闭的 '('。但是那部分代码不应该有一个 near 那里的任何地方。所以去找它( typedef struct op_instr (
  • @PaulR 如果我想使用联合,那么我需要在其中创建 3 个结构,一个用于 r 指令、i 指令和 j 指令的结构,对吗?

标签: c compiler-errors


【解决方案1】:

将结构定义中的“(”替换为“{”

typedef struct op_instr
**{**
   ...
**}**

编辑:你可能有这个problem

“基本上,普通的 C 预处理器指令、普通的 C 语言元素以及 Arduino IDE 和编译器链的高深莫测的内部结构之间存在复杂的交互。

据我所知,您可以将 #if 包装在简单声明和大多数可执行代码周围而不受惩罚,但是在条件句中放置比这更复杂的东西,例如简单的 typedef 结构会导致奇怪的问题。

事实上,只有 typedef 会导致问题,尤其是当您尝试在函数声明中使用随后的标记时。甚至不要考虑这些方面的任何事情:"

【讨论】:

    【解决方案2】:

    我认为你使用 ( 而不是 { 围绕你的结构。或者我错了吗?

    【讨论】:

    • 这是一个问题,但只是整个问题的一小部分。
    【解决方案3】:

    您的直接问题是您使用 () 而不是 {} 作为结构范围。

    正如 Paul R 所说,您似乎还有其他一些问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-12
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多