2012-04-17 25 views
0

Possible Duplicate:
「expected ':', ',', ';', '}' or 'attribute' before '{' token」 in Struct member function如何用Android NDK編譯結構與成員函數?

我使用android ndk-build編譯C++庫。當代碼中有一個定義了成員函數的結構時,我得到編譯錯誤。 此代碼與其他編譯器一起編譯。

示例代碼:

typedef struct 
{ 
    boolean current; 
    int a; 
    boolean IsCurrent() const 
    { 
     return current; 
    } 
} sampleStruct; 

我得到的錯誤是 - 「錯誤:預期 ':', ' ' '', '}' 或' 屬性' 前 '常量' 「 如果我擺脫常量的,錯誤是 - 」 錯誤:預期 ':', ' ' '', '}' 或' 屬性' 前 '{' 令牌」

+0

您正在使用哪種C++編譯器? – 2012-04-17 08:15:42

+0

布爾應該可能是布爾! – Stowelly 2012-04-17 09:32:53

回答

1

建議:

  1. boolean替換爲bool,因爲這是C++的布爾類型。
  2. 確保你沒有用C編譯器編譯。

風格建議: 請注意,typedef struct { ... } name;是在C++中的一個過時的做法。只需編寫struct name { ... };,它使您能夠編寫構造函數和析構函數。

+0

,並且請不要忘記最後一個';'在大括號'}'後面。 – hochl 2012-04-17 09:55:31

+0

@ hochl:哎呀,謝謝。我認爲這起源於我混合使用英語和C++,我忽略了';',因爲它不是完整表達式的結尾:P – 2012-04-17 10:24:23

+0

我希望你不介意我插入缺少的'...'以及;) – hochl 2012-04-17 10:38:43