中的标签进行静态结构初始化

中的标签进行静态结构初始化

本文介绍了使用C ++中的标签进行静态结构初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在stackoverflow上搜索了答案,但是找不到相关的东西.

I've searched stackoverflow for an answer but I cannot get something relevant.

我试图通过指定初始值来初始化具有初始值的静态结构实例,但是在编译时出现错误:

I'm trying to initialize a static structure instance with initial values by specifying their tags, but I get an error at compilation time:

src/version.cpp:10: error: expected primary-expression before ‘.’ token

代码如下:

// h
typedef struct
{
    int lots_of_ints;
    /* ... lots of other members */
    const char *build_date;
    const char *build_version;
} infos;

错误代码:

// C

static const char *version_date = VERSION_DATE;
static const char *version_rev  = VERSION_REVISION;

static const infos s_infos =
{
   .build_date    = version_date, // why is this wrong? it works in C!
   .build_version = version_rev
};

const infos *get_info()
{
    return &s_infos;
}

因此,基本思想是绕过其他成员"初始化,而仅设置相关的build_datebuild_version值.这曾经可以在C语言中使用,但我不知道为什么它不能在C ++中使用.

So the basic idea is to bypass the "other members" initialization and only set the relevant build_date and build_version values.This used to work in C, but I can't figure out why it won't work in C++.

有什么想法吗?

修改:

我意识到这段代码看起来像简单的C,实际上是这样.整个项目都是C ++,因此我必须使用C ++文件扩展名来防止makefile依赖关系混乱(%.o: %.cpp)

I realize this code looks like simple C, and it actually is. The whole project is in C++ so I have to use C++ file extensions to prevent the makefile dependency mess ( %.o: %.cpp )

推荐答案

您正在使用的功能是C99功能,并且您正在使用不支持该功能的C ++编译器.请记住,尽管C代码通常是有效的C ++代码,但C99代码并非总是如此.

The feature you are using is a C99 feature and you are using a C++ compiler that doesn't support it. Remember that although C code is generally valid C++ code, C99 code isn't always.

这篇关于使用C ++中的标签进行静态结构初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:30