0

I'm trying to convert some code from C to C++ This is a project using a raspberry pi with camera module and I'd like to analyse pictures with it.

But on this piece of code (somebody else created) I get this error

231:8: error: expected primary-expression before ‘.’ token

Which is this line:

.max_stills_w = state->width,

I tried everything I could find but it keeps giving me other errors

    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
Clifford
  • 88,407
  • 13
  • 85
  • 165
Matthew
  • 276
  • 2
  • 3
  • 15
  • 3
    This is a C99 feature and is not available in C++ as written. You would just instantiate your `struct` variable, then assign to the named members afterwards, you cannot initialize using the name members as written. – Cory Kramer Apr 22 '15 at 19:03
  • @Cyber Does that question explain how to do the equivalent in C++? If not, it's not a duplicate. – Barmar Apr 22 '15 at 19:05
  • You should not convert working C-code. I suggest wrapping the C-code with C++ (if it has development/maintenance benefits) –  Apr 22 '15 at 19:06
  • @Barmar No it doesn't, but if they understand what it is doing, they'll know how to do it in C++. Especially since I explained it in the comment following the duplicate link. – Cory Kramer Apr 22 '15 at 19:06
  • That's quite an assumption from someone who's been here as long as you have :) – Barmar Apr 22 '15 at 19:07
  • The duplicate does not explain how to do this in C++, reopening. – Clifford Apr 22 '15 at 19:31
  • @Cyber : But that is neither the only or even the best method of initialising structure members in C++, and comments are not intended for posting answers in any case. By closing the question down you got your "answer" in in a comment then prevented anyone else from posting an answer in the correct manner – Clifford Apr 22 '15 at 19:51
  • 1
    @DieterLücking : I would say "need not" rather than "should not", but it is a worthwhile point to make that C and C++ are interoperable, and it is often a waste of effort to "convert" C code rather then simply using it as-is, as you risk changing the semantics in ways that are not always obvious. And of course because C99 has diverged from C++ so it may just be hard work. – Clifford Apr 22 '15 at 19:55
  • @Cyber: Your suggestion is not a method of *initializing* members at all. There's a world of difference between initialization and assignment (even in C -- you cannot put those *assignment statements* outside of a function, though that appears not to be an issue in this specific case) – Ben Voigt Apr 22 '15 at 20:21
  • @Clifford: And then it is a duplicate after all. – Ben Voigt Apr 22 '15 at 20:23
  • @BenVoigt : It is a duplicate of the question you have identified - it was *not* a duplicate of the [question previously identified](http://stackoverflow.com/questions/8047261/what-does-dot-mean-in-a-struct-initializer) by Cyber as its duplicate. – Clifford Apr 23 '15 at 08:49

2 Answers2

2

C++ doesn't support named initializers, only positional ones.

You'll have to read the struct definition and arrange all the initializers into declaration order.

Any members which weren't being named in the C code were being initialized to zero implicitly. You may have to make some of them explicit in order to not skip positions.

If this code is from JVCleave then it appears that the members are already in the correct order, you can just comment out the names and it will still work.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
1

The named structure member initialisation in C99 is not implemented in C++. In C++ you can perform member initialisation using a constructor. E.g:

struct sConfig 
{
    int m_x ;
    int m_y ;

    sConfig( int x, int y ) : m_x(x),
                              m_y(y)
    {}
} ;

Or:

struct sConfig 
{
    int m_x ;
    int m_y ;

    sConfig( int x, int y )
    {
        m_x = x ;
        m_y = y ;
    }
} ;

or even a combination of the two methods. Then you instantiate an object thus:

sConfig myConfig( 10, 20 ) ;

Initialising m_x and m_y to 10 and 20 respectively in this example.

You can then of course only perform the initialisation provided by the structures defined constructors; but that is not necessarily a bad thing - letting the user arbitrarily decide which members to initialise is not particularly safe or maintainable. You can of course define multiple constructors to perform different initialisations; you would normally want define a default constructor for example:

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)
    {}
} ;

So that in this example:

sConfig myConfig ;

is equivalent to all of the following:

sConfig myConfig( 0, 0 ) ;
sConfig myConfig = { 0, 0 } ;
sConfig myConfig = {0} ;
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Aggregate initialization still works just fine in C++. – Ben Voigt Apr 22 '15 at 20:21
  • @BenVoigt : I have not said otherwise (although I edited to clarify - perhaps you are referring ot the pre-edited text?); you answer remains valid - I have chosen not to duplicate it. I would suggest that C99 has named member initialisation because it does not have constructors, and equally C++ does not have named member initialisation *because* it does have constructors. I am simply stating this method as the C++ method of selective member initialisation. – Clifford Apr 23 '15 at 08:40