c - 具有返回类型 typedef 结构的函数在 VS2015 中不起作用

标签 c visual-studio

我使用结构类型的 typedef 定义了一个类型:

// Declate a new struct LVAL
typedef struct {
    int type;
    long num;
    int err;
} lval;

然后尝试使用返回类型 lval 声明一个函数(在上面使用 typedef 创建):

// Create a new number type lval
lval lval_num(long x)
{
    lval v;
    v.type = LVAL_NUM;
    v.num = x;
    return v;
}

但是 Visual Studio 2015 没有编译它,没有显示错误,只有这两个:

Error   C2143   syntax error: missing ';' before '{'
Error   C2065   'x': undeclared identifier

任何人都可以发现错误/错误吗?我尝试了谷歌搜索和其他解决方法,但没有任何效果。 这是完整的代码片段:

    // Create enumerations of possible lval struct types
enum { LVAL_NUM, LVAL_ERR };

// Create enumerations of possible lval error types.
enum { LERR_DIV_ZERO, LERR_BAD_OP, LERR_BAD_NUM };

// Declate a new struct LVAL
typedef struct {
    int type;
    long num;
    int err;
} lval;

// Create a new number type lval
lval lval_num(long x)
{
    lval v;
    v.type = LVAL_NUM;
    v.num = x;
    return v;
}

我也尝试过这样声明该函数,但没有成功:

struct lval lval_num(long x)
{
    lval v;
    v.type = LVAL_NUM;
    v.num = x;
    return v;
}

编辑:这是在使用 gcc 的 LINUX 上工作,但在 Windows 上不工作(VS2015) 这是一个快照。 Code Snippet

最佳答案

我在您的代码中没有看到错误,所以错误一定是在这段代码之前。可能是另一个未终止的 { 或丢失的 ;

注意:语法错误没有“解决方法”。您必须修复语法错误。不幸的是,编译器可能非常不清楚实际错误发生的位置,只会告诉您哪里出错了。

关于c - 具有返回类型 typedef 结构的函数在 VS2015 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793865/

相关文章:

c - 在 C 中传递结构数组的元素

C - 为什么 getopt 在 Linux 上返回 255?

c - 我怎样才能更好地理解内核 C 编程代码

c# - .Where 列表上的 linq 查询在静态方法中失败了吗? (不,延迟评价)

visual-studio - 试图让 DataRow[] Debugger Visualizer 在 Visual Studio 2010 中工作

.net - 导出的 C 函数、结构和 .Net 编码(marshal)处理中 bool 的大小

c++ - 如何知道要在命令行中链接的库?

javascript - 通过 Grunt 任务注入(inject)内容,具体取决于 ASP.NET 项目构建配置

c - Visual Studio - 如何在Linux服务器上运行和编译C

c++ - 对二维线对执行操作的最佳方法是什么?