2012-11-15 161 views
0

我正在嘗試測試我的代碼到目前爲止,並且在編譯測試運行時出現錯誤。C錯誤預期標識符

這裏是我的代碼:

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' 

我的代碼有什麼問題?爲什麼我得到這個錯誤?

感謝

編輯:固定支架,以大括號

+2

你似乎有一種幻想,你可以使用預處理器條件在運行時動態定義你的結構嗎?考慮使用聯合來處理這種事情 –

+0

呵呵,好的非常感謝Paul –

+0

它可能有利於*閱讀錯誤信息任何時候當你看到一個'''期望的時候,你可以放心的是有一個未關閉的'''在那裏,但是那部分代碼不應該有一個在哪裏*在*附近。所以去找到它(''typedef struct op_instr('<<<< =====)並修復它。 – WhozCraig

回答

2

替換「(」在結構中的?定義 「{」

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

編輯:您可能是有這個problem

「基本上,普通C預處理器指令,普通C語言元素和Arduino IDE編譯器鏈中不可思議的內部之間存在複雜的交互。正如我所知道的那樣,如果圍繞簡單聲明和大多數可執行代碼肆無忌憚地進行包裝,但在條件內部放置比這更簡單的任何東西,比如簡單的typedef結構,會導致奇怪的問題。

事實上,只是typedef可能會導致問題,特別是如果您嘗試在函數聲明中使用隨後的標記。甚至不要去想沿着這些線路東西:」

+0

星爺沒有注意到它 感謝。 –

2

我想你使用(而不是{風靡你的結構還是我錯

+0

這是一個問題,但只能是整個問題的一小部分 –

0

你immidiate問題是,你使用的是(),而不是{}爲你的結構範圍

正如保羅 - [R說,你似乎有一些其他問題