Excess elements in scalar initializer

Hello, i’ve this macro to produce root files from raw data.
If I set

double delay[Nadc]= {0, 4.0720e4, 0, 4.0720e4};

but if I set the if conditions

 if ((strcmp(month, "JuneOctober22") == 0) ) delay[Nadc] = {0, 4.0720e4, 0, 4.0720e4};   // June2022 and October2022 delay
  else if ((strcmp(month, "February23") == 0)) delay[Nadc] = {0, 4.0270e4, 0, 4.0270e4};   //February 2023 delay

I get this error

maketree.cpp:102:61: error: excess elements in scalar initializer
  if ((strcmp(month, "JuneOctober22") == 0) ) delay[Nadc] = {0, 4.0720e4, 0, 4.0720e4};   // June2022 and October2022 delay
                                                            ^ ~~~~~~~~~~~~~~~~~~~~~~~~
maketree.cpp:103:62: error: excess elements in scalar initializer
  else if ((strcmp(month, "February23") == 0)) delay[Nadc] = {0, 4.0270e4, 0, 4.0270e4};   //February 2023 delay

maketree.cpp (8.0 KB)

Hi @faca87,

It seems that delay has been declared as an array of Nadc elements. Thus, the type of delay[i], being i an integer used to subscript the array, is a double.
However, you are trying to assign a std::initializer_list, i.e. a list of values, to one of such elements in the array, which of course is ill-formed.

I guess you instead intended to replace all the elements in the array, as in delay = { ... }; nonetheless, C++ does not allow array assignment, so you have basically two options:

  1. Declare delay as an std::array<double, Nadc>, which allows direct assignment, e.g.
std::array<double, 4> delay = {0, 4.0720e4, 0, 4.0720e4};
delay = {0, 4.0720e4, 0, 4.0720e4};
  1. Copy-assign element by element, either manually or by using std::copy().

Cheers,
J.

Thank you @jalopezg it works!

But, why my macro worked when I didn’t have the if condition, but it didn’t work using the if?
Fausto

That’s weird, as in: it’s ill-formed C++ and I am pretty sure that every compliant compiler would complain. Are you absolutely sure that there were no more changes in the code?

Cheers,
J.

Yes, the if condition was the only change…indeed, before of your help, deleting the if, the code worked again!

@jalopezg What’s “ill-formed” in:

const int Nadc = 4;
double delay[Nadc] = {0, 4.0720e4, 0, 4.0720e4}; // aggregate initialization

Sure. IIUC how your code looks before / after the change, before the change, the array is initialized and it is not assigned-to again. That is perfectly legal. :slight_smile:

@Wile_E_Coyote, nothing if the array is just initialized; I was actually referring to using copy-assignment after the delay[] array has been initialized (see above).

Cheers,
J.

1 Like

thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.