Return 语句在 Visual Studio 2013 中需要额外的分号

Return statement requires extra semicolon in Visual Studio 2013

我正在 Visual C++ 2013 中编译一些 C 代码(它必须用 C 编写)。

void drawDebugLines(vec2f pos, DebugControls *controls, vec2f dydx, vec2f p)
{
    static float lineSize = 0.05f;
    if (!controls->normalFlag && !controls->tangentFlag)
    {
        return;
    }
    vec3f nColor = cVec3f(1, 1, 0);
    ...

按原样,此代码将编译并且 运行 就好了。

如果我去掉那些花括号,我会得到以下编译错误:

error C2275: 'vec3f' : illegal use of this type as an expression

编辑:语句 vec3f nColor

出现此错误

但是,如果我在 return 语句之后总共添加了 两个 个分号(并且没有大括号),代码也会编译并且 运行就好了

这只发生在 Visual C++ 中,gcc 将编译此代码而不需要大括号或额外的分号。

我的问题是为什么 Visual C++ 这样做?我很高兴接受答案 "Visual Studio's C compiler is s***" 但 why/how 是 s*** 吗?

我应该指出,在我的头文件中,我包含了所有内容

#if __cplusplus
extern "C" {
#endif
...
#if __cplusplus
}
#endif

我不确定这是否相关,但我想我应该提一下以防万一。

编辑:您可以将代码放入文件并见证魔法:

额外编辑:文件名的扩展名 必须 是 .c,如果是 .cpp 则代码可以编译。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct vec3f vec3f;

struct vec3f
{
    float x, y, z;
};

vec3f cVec3f(float x, float y, float z)
{
    vec3f v;
    v.x = x;
    v.y = y;
    v.z = z;
    return v;
}

void drawDebugLines(vec3f pos, bool debug)
{
    if (!debug)
        return;;

    vec3f nColor = cVec3f(1, 1, 0);

    printf("nColor: %f %f %f\n", nColor.x, nColor.y, nColor.z);
}


int main(int argc, char **argv)
{
    vec3f pos = cVec3f(0, 0, 0);
    drawDebugLines(pos, true);

    return EXIT_SUCCESS;
}

去掉多余的分号,看它断了。如果它对您没有影响,而只是我的 Visual C++ 版本失败,那么老实说,这对我来说最有意义。

这是 Visual Studio 2013 年 C 编译器中的错误。已在 Visual Studio 2013 Update 2 中修复。