C++: array initializer, member initialization

Thread Starter

russian

Joined Sep 1, 2012
56
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?
 

Thread Starter

russian

Joined Sep 1, 2012
56
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?
 

Thread Starter

russian

Joined Sep 1, 2012
56
Rich (BB code):
class trigger_shape_helper {
    int pinStates1[PWM_PHASE_MAX_COUNT];
    int pinStates2[PWM_PHASE_MAX_COUNT];
public:
    single_wave_s waves[2] = {single_wave_s(pinStates1), single_wave_s(pinStates2)};
};
Here comes the same
Rich (BB code):
./firmware/controllers/trigger/trigger_structure.h:78:27: error: a brace-enclosed initializer is not allowed here before '{' token
../firmware/controllers/trigger/trigger_structure.h:78:80: warning: ISO C++ forbids initialization of member 'waves' [-fpermissive]
../firmware/controllers/trigger/trigger_structure.h:78:80: warning: making 'waves' static [-fpermissive]
../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]'
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?
 

Thread Starter

russian

Joined Sep 1, 2012
56
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
 
Top