Trying to define a static constant variable in a class

c++, class, static

Solution

You declare it inside the class body:

class ADC{
private:
    static const unsigned char adc_cmd[9];
//...
};

and define (and initialize) it outside (just once, as for any external-linkage definition):

const unsigned char ADC::adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 };

without writing `static`, as specified by the error message.

(don't ask me for an explanation of why here `static` is forbidden, I always found the various "repetition of qualifiers" rules completely illogic)

Problem

I am defining a variable `adc_cmd[9]` as a `static const unsigned char` in my class `ADC` under private. Since it is a constant I thought I would just define it inside the class it self, but that didn't work apparently: ``` #pragma once class ADC{ private: static const unsigned char adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 }; //... }; ``` Error: ``` error: a brace-enclosed initializer is not allowed here before '{' token error: invalid in-class initialization of static data member of non-integral type 'const unsigned char [9]' ``` ... So I tried bringing the line out of the class with: `static const unsigned char ADC::adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 };`, but that yielded this error: ``` error: 'static' may not be used when defining (as opposed to declaring) a static data member error: 'const unsigned char ADC::adc_cmd [9]' is not a static member of 'class ADC' ``` I obviously am not declaring this properly. What is the correct way to declare this?

Original source