[英]why type casting on non-pointer struct give syntax error
I am using Visual C++ express 2008 try to compile code similar to below:我正在使用 Visual C++ express 2008 尝试编译类似于以下的代码:
no problem没问题
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:但出现问题如下:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:给出了错误:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:其中 AVRational(ffmpeg.org 库)定义为:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as FFmpeg 带有一些预定义的值,例如
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below如下使用
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008将无法在 Visual C++ express 2008 上编译
It seem like the same code will be compiled with no error/warning on gcc compiler.似乎在 gcc 编译器上编译相同的代码时不会出现错误/警告。 Why I get this error on VC++?
为什么我在 VC++ 上收到这个错误? Is it a C/C++ standard way to do casting on struct value?
它是对结构值进行强制转换的 C/C++ 标准方法吗? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
无论如何,我可以在仍然能够使用定义的 AV_TIME_BASE_Q 的同时避免这个错误?
Use av_get_time_base_q()
instead of AV_TIME_BASE_Q
for C++ or VS.对于 C++ 或 VS,使用
av_get_time_base_q()
而不是AV_TIME_BASE_Q
。 This was fixed in a patch 这是在补丁中修复的
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options: VC++ 2013 不允许在 C++ 中使用复合文字,但在 C 中允许它们。选项:
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file如果您希望保留 C++,另一种选择是更改头文件中 AV_TIME_BASE_Q 的声明
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.然后它将使用常量而不是复合文字。
For compound-literals errors in C++对于 C++ 中的复合字面量错误
wrong:错误的:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:正确的:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.