user1283776
user1283776

Reputation: 21764

Initialize a static variable that is of struct type?

First I only had one recording each of voltageSamples, currentSamples, energySamples:

static QList<qreal> voltageSamples{
    3091, 3085, 2911, 3048
};
static QList<qreal> currentSamples{
    4152956, 5341953, 7330711, 3425060,
    5382685, 9864420, 6313012, 3024116,
    6382968, 4411640, 7584845, 9992644,
    9541896, 7608791, 7332224, 3368969
};
static QList<qreal> energySamples{
    8.914435413888887e-07, 2.038108591597222e-06, 3.611666070833333e-06, 4.346864713888889e-06,
    5.500030215625000e-06, 7.613345194791666e-06, 8.965820335069444e-06, 9.613695186458332e-06,
    1.090402989812500e-05, 1.179585517868056e-05, 1.332915266444444e-05, 1.534919340638889e-05,
    1.736889472638889e-05, 1.897942215472222e-05, 2.053140956805556e-05, 2.124450800638889e-05
};

Then I needed to add multiple samples. I'm not great at C++, but my guess at a good approach was to throw the three variables above into a struct and then initialize multiple static variables of said struct.

This was my best attempt:

struct recording {
    QList<qreal> voltageSamples;
    QList<qreal> currentSamples;
    QList<qreal> energySamples;
};

static recording r1;

r1.voltageSamples = {3091, 3085, 2911, 3048};

r1.currentSamples{
    4152956, 5341953, 7330711, 3425060,
    5382685, 9864420, 6313012, 3024116,
    6382968, 4411640, 7584845, 9992644,
    9541896, 7608791, 7332224, 3368969
};

r1.energySamples{
    8.914435413888887e-07, 2.038108591597222e-06, 3.611666070833333e-06, 4.346864713888889e-06,
    5.500030215625000e-06, 7.613345194791666e-06, 8.965820335069444e-06, 9.613695186458332e-06,
    1.090402989812500e-05, 1.179585517868056e-05, 1.332915266444444e-05, 1.534919340638889e-05,
    1.736889472638889e-05, 1.897942215472222e-05, 2.053140956805556e-05, 2.124450800638889e-05
};

static recording r2;
...

But my code does not work. What am I doing wrong?

Upvotes: 0

Views: 104

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385174

You switched from initialisation to assignment, but tried to keep using initialisation syntax. That's just not possible.

If you add the = in to the currentSamples and energySamples then the assignments may be valid (I don't know enough about QList<qreal>'s design to be sure that this is the case, but it doesn't actually matter because, well, see below).

Furthermore, your assignments are just floating freely in empty space, but statements/actions like these must be in a function (e.g. main).

Finally, you should really be using an array for something like this, not a bunch of numbered variables. And if you use an array, then you can stick with initialisers!

static QList<qreal> voltageSamples[] = {
    {3091, 3085, 2911, 3048},
    { /*(next sample data)*/ }
};
static QList<qreal> currentSamples[] = {
    {
       4152956, 5341953, 7330711, 3425060,
       5382685, 9864420, 6313012, 3024116,
       6382968, 4411640, 7584845, 9992644,
       9541896, 7608791, 7332224, 3368969
    },
    { /*(next sample data)*/ }
};

and so on.

Or, keeping with the recording type (which is reasonable):

static recording recordings[] = {
   {  // recording 1
      {3091, 3085, 2911, 3048},  // voltage samples
      {  // current samples
        4152956, 5341953, 7330711, 3425060,
        5382685, 9864420, 6313012, 3024116,
        6382968, 4411640, 7584845, 9992644,
        9541896, 7608791, 7332224, 3368969
      },
      {  // energy samples
        8.914435413888887e-07, 2.038108591597222e-06, 3.611666070833333e-06, 4.346864713888889e-06,
        5.500030215625000e-06, 7.613345194791666e-06, 8.965820335069444e-06, 9.613695186458332e-06,
        1.090402989812500e-05, 1.179585517868056e-05, 1.332915266444444e-05, 1.534919340638889e-05,
        1.736889472638889e-05, 1.897942215472222e-05, 2.053140956805556e-05, 2.124450800638889e-05
      }
   },
   {  // recording 2
      /* etc */
   }
};

The key here is that initialisers can nest.

Upvotes: 6

Related Questions