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

Bla*_*arf 5 c visual-c++ 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);
    ...
Run Code Online (Sandbox Code Playgroud)

原样,这段代码将编译并运行得很好.

如果我拿出那些花括号,我得到以下编译错误:

error C2275: 'vec3f' : illegal use of this type as an expression
Run Code Online (Sandbox Code Playgroud)

编辑:语句发生此错误 vec3f nColor

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

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

我的问题是为什么Visual C++会这样做?我很高兴接受"Visual Studio的C编译器是s***"的答案但是为什么/它是怎么回事?

我应该指出,在我的头文件中,我把所有东西都包裹在里面

#if __cplusplus
extern "C" {
#endif
...
#if __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

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

编辑:代码你可以扔进一个文件,见证魔术:

额外编辑:文件名的扩展名必须是.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;
}
Run Code Online (Sandbox Code Playgroud)

拿出额外的分号,看看它是否破裂.如果它没有为你而破坏,那只是我的Visual C++版本失败了,那么说实话,这对我来说是最有意义的.

Jam*_*lis 6

这是Visual Studio 2013中C编译器中的一个错误.它已在Visual Studio 2013 Update 2中修复.