2012-04-17 60 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