【发布时间】: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