static const array as class member

Greetings, I am trying to create a static const double array as a class member. It is basically part of a lookup table that I want to target into FLASH. Not really sure what is wrong here, as my code compiles fine in a different compiler (IAR). Here is from my *.h file (I replaced most of the code with "..." so you could easily focus on the culprit member) :

class ELF_SOC
{
public:
...
private:
...
      static const double socSamples[];
...
};

This is from my *.cpp file:

...
const double ELF_SOC::socSamples[] = {0.0, 0.1666, 0.3333, 0.5, 0.6667, 0.8333, 0.9875, 1.0};
...
ELF_SOC::ELF_SOC(double samplingPeriod)
{
	m_socAlgo = new NonUpdatingObserver((double*) &socSamples[0], ...);
...
}

The error I am receiving says "two or more data types in declaration of 'socSamples'" and it is pointing to the const declaration in my cpp file.

Any help? I thought that maybe the Arduino compiler needed the array size explicitly stated ([8]), but that did not help.

You are declaring the array, incorrectly since there is no size defined, in the header file and again in the source file. That is NOT allowed. Declared it in the header file. Initialize it in the source file. And, no, you can't initialize it using the declare and initialize in one step process in the header file.

Like I said, I tried it with "8" in the brackets (both *.h and *.cpp file), and I got the same exact error.

Declared it in the header file.

I did

Initialize it in the source file.

I did

And, no, you can't initialize it using the declare and initialize in one step process in the header file.

My code didn't attempt to do that.

Does something else need to change from my given example?

No, it would be working if you did that ( without other errors ).

.h

struct Test{
  static const double socSamples[  8 ];
};

.cpp

const double Test::socSamples[ ] = {0.0, 0.1666, 0.3333, 0.5, 0.6667, 0.8333, 0.9875, 1.0};

Compiles fine.

However if I write this silly code:

int
const double Test::socSamples[ ] = {0.0, 0.1666, 0.3333, 0.5, 0.6667, 0.8333, 0.9875, 1.0};

you get 'error: two or more data types in declaration of 'socSamples''

Look at the code above the array, probably a missiing ';'

Well I don't have anything above the array initialization except #include statements (no silly errors like your example)....but if you say that there is nothing illegal about the code I provided then that is at least helpful to me in that I should look elsewhere for an error (maybe those included files). Maybe it's a case of a missing bracket or semi-colon.

Something still seems suspicious to me in the fact that I could compile the code fine in IAR. Oh well, thank you very much friendly Arduino community.

#includes are copy n paste operations,

look at the code in the header as it is included above your code :slight_smile:

Maybe it's a case of a missing bracket or semi-colon.

Yup, that's what I was pointing out.

I recall some funnybusiness with the static keyword, you can have it it the header file or the cpp file, but not both.

I also think you are better off explicitly defining the size of the array in the class declaration. The definition of the class functions don't all have to be in the same source code file, I can forsee issues if you want to have a static class member where the size might not be known within the compilation scope of a particular source code file.

michinyon:
I recall some funnybusiness with the static keyword, you can have it it the header file or the cpp file, but not both.

static can only go on declarations, not definitions, in other words it cannot go in the .cpp if/or outside the class. Think of them as 'with reference' to the scope they are declared in.

struct Foo{
  typedef int A;
  static A a;
};

static Foo::A a = 0;
Foo::A Foo::a = 0;

Foo::a is static to Foo, a is static to the file & not part of the class.