C++: array initializer, member initialization

Discussion in 'Programmer's Corner' started by russian, Apr 29, 2014.

  1. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    The lack of formal C++ training hits me, I am failing to express myself so that my compiler is happy :)

    I want trigger_shape_s to initialize an array of two single_wave_s references and pass that array to a constructor of a member wave.

    I am trying

    class trigger_shape_s {
    public:
    trigger_shape_s();
    multi_wave_s wave;
    private:
    single_wave_s waves[2];
    single_wave_s sr[2] = {waves[0], waves[1]};
    };

    trigger_shape_s::trigger_shape_s() :
    wave(sr) {
    }

    class multi_wave_s {
    public:
    multi_wave_s(single_wave_s *waves);
    }

    With a GCC 4.8.1 on Windows I've got a warning suggesting I use -std=c++11. Fine, I did that and the code compiles on Windows.

    The funny thing is that my Unix 4.8.3 build server which uses the same exact Makefile is not happy:
    ./firmware/controllers/trigger/trigger_structure.h:96:24: error: a brace-enclosed initializer is not allowed here before '{' token
    ../firmware/controllers/trigger/trigger_structure.h:96:43: warning: ISO C++ forbids initialization of member 'sr' [-fpermissive]
    ../firmware/controllers/trigger/trigger_structure.h:96:43: warning: making 'sr' static [-fpermissive]
    ../firmware/controllers/trigger/trigger_structure.h:96:43: error: invalid in-class initialization of static data member of non-integral type 'single_wave_s [2]'


    Just to be sure - as far as I understand 'static' attribute I do NOT want any members here to be static - I want each instance of trigger_shape_s to hold it's own array.

    Please advice - how do I properly implement the thing I am trying to implement?
     
  2. dougy83

    Member

    May 11, 2011
    11
    4
    What's wrong with typing
    Code ( (Unknown Language)):
    1. trigger_shape_s::trigger_shape_s() :
    2. wave(waves) {
    3. }
    and removing sr from trigger_shape_s?
     
    russian likes this.
  3. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    Ops. Somehow I've missed that, thank you for helping me out :)

    In general, is the order of execution specified? wave() constructor is probably executed before the body of trigger_shape_s() constructor? Is there a way to properly implement this with 'sr' - meaning some trigger_shape_s initialization code before wave() constructor?
     
  4. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    Code ( (Unknown Language)):
    1. class trigger_shape_helper {
    2.     int pinStates1[PWM_PHASE_MAX_COUNT];
    3.     int pinStates2[PWM_PHASE_MAX_COUNT];
    4. public:
    5.     single_wave_s waves[2] = {single_wave_s(pinStates1), single_wave_s(pinStates2)};
    6. };
    7.  
    Here comes the same
    Code ( (Unknown Language)):
    1. ./firmware/controllers/trigger/trigger_structure.h:78:27: error: a brace-enclosed initializer is not allowed here before '{' token
    2. ../firmware/controllers/trigger/trigger_structure.h:78:80: warning: ISO C++ forbids initialization of member 'waves' [-fpermissive]
    3. ../firmware/controllers/trigger/trigger_structure.h:78:80: warning: making 'waves' static [-fpermissive]
    4. ../firmware/controllers/trigger/trigger_structure.h:78:80: error: invalid in-class initialization of static data member of non-integral type 'single_wave_s [2]'
    5.  
    Looks like I was wrong about the gcc versions on my unix build server: it seems like this compiles as ARM target on 4.8.3 and does NOT compile with native target with 4.6.3. Weird? Expected?
     
  5. russian

    Thread Starter Member

    Sep 1, 2012
    55
    1
    Update: the upgrade 4.6.3 to 4.8.1 has resolved the "error: a brace-enclosed initializer is not allowed here before '{' token" error!

    this is somewhat unexpected
     
Loading...