【问题标题】:C syntax - error C2143: syntax error : missing ')' before '*' [duplicate]C 语法 - 错误 C2143:语法错误:在 '*' 之前缺少 ')' [重复]
【发布时间】:2016-12-01 21:50:39
【问题描述】:

为什么我的 C 标头声明出现语法错误?

这是我的头文件,viterbi.h:

#ifndef VITERBI_H
#define VITERBI_H
void vitdec(float* , int , int ,   bool* );
#endif //VITERBI_H

这是我的实现文件,viterbi.c:

// viterbi.c   : Defines the entry point for the console application.
//
#include "viterbi.h"
#include "math.h"
//void vitdec(float* sd, int frameLen, int rate,   bool* hd);

void vitdec(float* sd, int frameLen, int rate,   bool* hd)
{
    //... The rest of the function

Visual Studio 2010 编译器的错误如下:

viterbi.h(4): error C2143: syntax error : missing ')' before '*'
viterbi.h(4): error C2081: 'bool' : name in formal parameter list illegal
viterbi.h(4): error C2143: syntax error : missing '{' before '*'
viterbi.h(4): error C2059: syntax error : ')'
viterbi.h(4): error C2059: syntax error : ';'
viterbi.c(7): error C2065: 'bool' : undeclared identifier
viterbi.c(7): error C2065: 'hd' : undeclared identifier
viterbi.c(7): warning C4552: '*' : operator has no effect; expected operator with side-effect

据我所见/所知,这是 C 声明的有效语法。如果我将 viterbi.c 编译为 C++ 代码 (viterbi.cpp),那么错误就会消失。什么是语法错误?

【问题讨论】:

  • C (89) 中没有 bool 这样的东西。您想使用 Visual Studio 编译 C 代码,那么您必须编写 C 代码,而不是 C++ 代码。
  • 看起来bool 可能是问题所在:stackoverflow.com/a/18042253
  • @PaulMcKenzie:bool 是一个宏映射到标准类型 _Bool。并且有建议不要在应用程序中重新定义bool 等。而且C不是C89,而是标准C,只有 C11。
  • C 和 C++ 是不同的语言。不要将 C 代码编译为 C++,反之亦然。如果要使用 C,请不要使用 MSVC,它不符合标准。它被 27 岁的过时版本 C89/90 卡住了。像 gcc 或 clang 这样的现代 C 编译器确实支持该标准(要使用 bool,您必须根据标准使用 #include <stdbool.h>
  • 鉴于错误消息,使用了 @Olaf Visual Studio。 Visual Studio 附带的C 编译器仅兼容 C89。

标签: c++ c visual-studio-2010


【解决方案1】:

bool 不是原生 C 类型,但对于使用 C99 的用户,请尝试添加行 #include <stdbool.h>,其中包含定义 bool 的宏。

由于所有 Visual Studio/MSVC 产品中的 C 编译器都使用 C89,因此根本没有为您定义 bool,作为本机 C 类型或其他类型。变通方法包括使用typedefenum 定义bool。示例在下面的链接中。

欲了解更多信息,请参阅:Is bool a native C type?

【讨论】:

  • bool 不是本地的,但它通过宏映射到该标头中的本地类型 _Bool(强制)。那是因为在野外有太多写得很糟糕的 C 代码定义了他们自己的 bool 版本(您可能会惊讶于某些人如何定义他们自己的布尔类型)。所以你可以使用_Bool 作为原生类型(虽然我不推荐它)。
  • @Keith 我的 VS 2010 编译器支持 C89/90,所以我必须寻找其他解决方案。
  • @StephenWang 这就是我的意思,你需要解释宏奥拉夫的标题是可见的。你试过了吗?在没有包含的情况下在 ideone.com 上编译失败并成功。
  • @KeithM 正确, 使 _Bool 宏可见。 _Bool 内置类型仅为 C99 定义 - 请参阅 bool-data-type-of-c99-c。因此对于 VS 2010 的安装,既没有安装 也没有 _Bool 内置类型。
  • @StephenWang 啊,那么如果您认为链接的重复项没有回答您的问题,您应该编辑您的问题以解释为什么它不是重复项,然后请求重新打开它. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2019-07-07
  • 2011-05-17
  • 1970-01-01
相关资源
最近更新 更多