2012-07-04 181 views
0

somefile.h的內容:C代碼的編譯錯誤

#ifndef __SOMEFILE_H 
#define __SOMEFILE_H 

#ifdef __cplusplus 
extern "C" { 
#endif 

typedef struct _table_t 
{ 
    void (*somefunction1)(); 
    void (*somefunction2)(int a); 
    void (*somefunction3)(int a, int *b); 
}table_t; 

void doSomething1(); 
void doSomething2(); 

#ifdef __cplusplus 
} // error at this line: expected constructor, destructor, or type conversion before '(' token 
#endif 
#endif 

上面顯示的是代碼片段和錯誤,當我編譯我在Linux上的代碼,我得到。沒有任何抱怨,相同的代碼在Windows上編譯得很好。請幫我解決這個問題。我在互聯網上搜索了很多,但沒有找到正確的解決辦法。謝謝。

關於源文件:

all.h is a header file which includes: 
#include "header1.h" 
#include "header2.h" 
#include "header3.h" 
#include "somefile.h" 

這裏是somefile.c

#include "all.h" 
#include "header4.h" 
jumptable_t jumptable_a = 
{ 
     a_function1(); 
     a_function2(int a); 
     a_function3(int a, int *b); 
} 

//more code 
void function1() 
{ 
    a_function1(); 
} 

void function2(int a) 
{ 
    a_function2(a); 
} 

void function3(int a, int *b) 
{ 
    a_function3(a, b); 
} 


void doSomething1() 
{ 

} 
void doSomething2() 
{ 

} 
+0

假設這個頭文件被包含在一個C++源文件中,那麼它上面是否還有其他頭文件? –

+0

是的,這個頭文件包含在C++源文件中。 – RRR

+0

不,上面沒有其他頭文件。 – RRR

回答

1

宏與領先的雙下劃線是非法的內容。你需要改變你的防守。

+0

請讓我知道你是什麼意思 - 你需要改變你的包括後衛。謝謝。 – RRR

+0

@RRR,'#ifndef __SOMEFILE_H #define __SOMEFILE_H'這包括警衛。 – SingerOfTheFall

0

您需要;}jumptable_a。在初始化程序jumptable_a中使用逗號代替分號。

大括號使它看起來像一個思維類似功能,但事實並非如此。

此外,在somefile.h該結構被稱爲table_t,但在somefile.c您使用jumptable_t,我以爲是在這裏寫帖子的時候引入了一個錯誤。

+0

即使在這些更改之後,初始化器仍然無效,它確實沒有任何類似於有效的代碼。可能他的意思是「{&function1,&function2,&function3}'作爲初始化程序(當然這些函數是首先聲明的) –

+0

Oh right。我首先看到了缺少的分號,然後是缺少的逗號,並錯過了其餘的東西。 幹得好,我。 –