【问题标题】:two or more data types in declaration specifiers声明说明符中有两种或多种数据类型
【发布时间】:2011-08-21 14:56:27
【问题描述】:

我环顾四周并尝试了一些东西,目前没有任何效果。

main.c:13: error: two or more data types in declaration specifiers
make[1]: *** [main.o] Error 1
make: *** [build] Error 2

我的代码差不多就是这个(我已经把所有的东西都注释掉了,所以它不是别的东西+除此之外没有其他文件);

ma​​in.h

struct savetype{
    bool file_exists;
}

ma​​in.c

#include "main.h"
extern struct savetype save;
int main (void){
return 0;
}

stuff.c

#include "main.h"
struct savetype save;
save.file_exists=true;

【问题讨论】:

  • 另外,如果我在 main.h 中向“struct savetype{/*stuff*/}”添加分号,我会收到此错误:stuff.c:4: error: expected '=', ',', ' ', 'asm' 或 'attribute' 在 '.' 之前令牌
  • 我不相信 C 中有 bool 数据类型。
  • @Rafe:c99 有一个原生的bool 类型,现在大多数编译器都支持它。
  • @dmckee: bool 仅在您拥有#include <stdbool.h> 时可见。

标签: c declaration specifier


【解决方案1】:

C 结构声明必须以分号结尾。在main.h 中的结构声明末尾加一个分号就可以了。

另外,应该有一个bool 类型可供您使用,除非您有其他代码定义它。在 C 中,使用 int 而不是布尔值。

此外,标准 C 中没有 true 这样的东西; 0 为假,其他为真,因此您还必须更正 stuff.c。

此外,stuff.c 不应该编译,因为它包含任何函数之外的代码(不仅仅是声明)(特别是 save.file_exists = true;

【讨论】:

    【解决方案2】:

    这就是问题所在:

    struct savetype{
        bool file_exists;
    };
    ^^^^ <-------------- Here!
    

    最后你忘了分号。

    【讨论】:

      【解决方案3】:

      C 中不存在 bool 类型。您可以使用宏以方便的方式使用 TRUE/FALSE 值:

      #define TRUE 1
      #define FALSE 0
      

      那么你可以在这样的条件语句中使用它:

      if (var == TRUE){
      
      }
      

      如果您想使用 'bool' 作为关键字:

      typedef int bool;
      

      编辑:

      我不知道,但@Bo Persson 指出从 C99 开始引入布尔类型。要使用它们,请包含以下原型:

      #include <stdbool.h>
      

      【讨论】:

      • @Bo Persson:谢谢。我不知道。你认为我应该删除我的答案吗?
      • 如果不正确,您可以修复或删除它。这取决于你!
      • if (var == TRUE) 是不必要且危险的。如果var 代表一个条件,只需写if (var)——并给它一个有意义的名称。请记住,any 非零值我们在一个条件下为真。
      猜你喜欢
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 2010-09-18
      • 2012-07-15
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多