3

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?

0

5 Answers 5

5

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)

Sign up to request clarification or add additional context in comments.

Comments

5

In C++03, static data member definitions go outside of the class definition.

Header:

#pragma once

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

In one .cpp file:

#include "headername"

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

Comments

3

Combine the two:

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

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

Comments

2

Just to contribute to Matteo's answer:

We must point out that the static qualifier is indeed a bit confusing in C++. To my knowledge, it does three different things depending on where it is used:

  1. In front of class member attributes: makes this attribute the same for all instances of that class (same as Java).
  2. In front of a global variable: reduces the scope of the variable to the current source file only (same as C).
  3. In front of a local variable within a method / function: makes this variable the same for all calls to that method / function (same as C, may be useful for the singleton design pattern).

Comments

1

Since C++11, what you can do in a case like this (when you have a static const LiteralType) is use constexpr, making the value a guaranteed compile-time constant.

That would be:

#pragma once

class ADC{
private:
    static constexpr unsigned char adc_cmd[9] = { 0x87, 0xC7, 0x97, 0xD7, 0xA7, 0xE7, 0xB7, 0xF7, 0x00 };
//...
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.