将代码从 C 转换为 C++
Converting code from c to c++
我正在尝试将一些代码从 C 转换为 C++
这是一个使用带相机模块的 raspberry pi 的项目,我想用它分析图片。
但是在这段代码(其他人创建的)上我得到了这个错误
231:8: error: expected primary-expression before ‘.’ token
这是哪一行:
.max_stills_w = state->width,
我尝试了所有我能找到的方法,但它总是给我其他错误
video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
// set up the camera configuration
{
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config =
{
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof(cam_config) },
.max_stills_w = state->width,
.max_stills_h = state->height,
.stills_yuv422 = 0,
.one_shot_stills = 0,
.max_preview_video_w = state->width,
.max_preview_video_h = state->height,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
};
mmal_port_parameter_set(camera->control, &cam_config.hdr);
}
// Set the encode format on the video port
C99 中的命名结构成员初始化未在 C++ 中实现。在 C++ 中,您可以使用构造函数执行成员初始化。例如:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y ) : m_x(x),
m_y(y)
{}
} ;
或者:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y )
{
m_x = x ;
m_y = y ;
}
} ;
甚至是两种方法的结合。然后你实例化一个对象:
sConfig myConfig( 10, 20 ) ;
在本例中将m_x
和m_y
分别初始化为10和20。
您当然可以只执行由结构定义的构造函数提供的初始化;但这不一定是坏事——让用户任意决定初始化哪些成员不是特别安全或可维护。您当然可以定义多个构造函数来执行不同的初始化;您通常希望定义一个默认构造函数,例如:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y ) : m_x(x),
m_y(y)
{}
sConfig( ) : m_x(0),
m_y(0)
{}
} ;
所以在这个例子中:
sConfig myConfig ;
等同于以下所有内容:
sConfig myConfig( 0, 0 ) ;
sConfig myConfig = { 0, 0 } ;
sConfig myConfig = {0} ;
C++ 不支持命名初始化程序,仅支持位置初始化程序。
您必须阅读 struct
定义并将所有初始值设定项按声明顺序排列。
任何未在 C 代码中命名的成员都被隐式初始化为零。为了不跳过位置,您可能必须明确其中的一些。
如果此代码来自 JVCleave,那么成员的顺序似乎已经正确,您只需注释掉名称,它仍然有效。
我正在尝试将一些代码从 C 转换为 C++ 这是一个使用带相机模块的 raspberry pi 的项目,我想用它分析图片。
但是在这段代码(其他人创建的)上我得到了这个错误
231:8: error: expected primary-expression before ‘.’ token
这是哪一行:
.max_stills_w = state->width,
我尝试了所有我能找到的方法,但它总是给我其他错误
video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
// set up the camera configuration
{
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config =
{
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof(cam_config) },
.max_stills_w = state->width,
.max_stills_h = state->height,
.stills_yuv422 = 0,
.one_shot_stills = 0,
.max_preview_video_w = state->width,
.max_preview_video_h = state->height,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
};
mmal_port_parameter_set(camera->control, &cam_config.hdr);
}
// Set the encode format on the video port
C99 中的命名结构成员初始化未在 C++ 中实现。在 C++ 中,您可以使用构造函数执行成员初始化。例如:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y ) : m_x(x),
m_y(y)
{}
} ;
或者:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y )
{
m_x = x ;
m_y = y ;
}
} ;
甚至是两种方法的结合。然后你实例化一个对象:
sConfig myConfig( 10, 20 ) ;
在本例中将m_x
和m_y
分别初始化为10和20。
您当然可以只执行由结构定义的构造函数提供的初始化;但这不一定是坏事——让用户任意决定初始化哪些成员不是特别安全或可维护。您当然可以定义多个构造函数来执行不同的初始化;您通常希望定义一个默认构造函数,例如:
struct sConfig
{
int m_x ;
int m_y ;
sConfig( int x, int y ) : m_x(x),
m_y(y)
{}
sConfig( ) : m_x(0),
m_y(0)
{}
} ;
所以在这个例子中:
sConfig myConfig ;
等同于以下所有内容:
sConfig myConfig( 0, 0 ) ;
sConfig myConfig = { 0, 0 } ;
sConfig myConfig = {0} ;
C++ 不支持命名初始化程序,仅支持位置初始化程序。
您必须阅读 struct
定义并将所有初始值设定项按声明顺序排列。
任何未在 C 代码中命名的成员都被隐式初始化为零。为了不跳过位置,您可能必须明确其中的一些。
如果此代码来自 JVCleave,那么成员的顺序似乎已经正确,您只需注释掉名称,它仍然有效。