0

I'd like to have a static (const) vector in a static class. The vector wont be filled with new elements, it will only have the elements defined in class configuration: {1,2,3,4,5}.

How can I make the vector debugLevels static?

#include <ArduinoSTL.h>

class configuration {
  public:
  static std::vector<int> const debugLevels = {1,2,3,4,5}; // throws error: in-class initialization of static data member 'const std::vector<int> configuration::debugLevels' of incomplete type
};

void setup() {
  for(int i=0; i<configuration::debugLevels.size(); i++ {
    // do some stuff here...
  }
}

void loop() {
}
Fareanor
  • 5,900
  • 2
  • 11
  • 37
alve89
  • 971
  • 1
  • 9
  • 31
  • there are no static classes in c++. You can have a class with only static members, but thats just globals in disguise – 463035818_is_not_an_ai Mar 09 '20 at 15:31
  • 2
    Does this answer your question? [How to initialize private static members in C++?](https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c) – Fareanor Mar 09 '20 at 15:31
  • @Fareanor No, I don't want to make it private. I want it to be public but statically callable from outside of the class. – alve89 Mar 09 '20 at 15:33
  • @alve89 the `private` is irrelevant, the same answer can apply – 463035818_is_not_an_ai Mar 09 '20 at 15:34
  • @idclev463035818: Okay, then it's a class with static members. :-) – alve89 Mar 09 '20 at 15:34
  • @alve89 It is not about `private` but about `static` member initialization, this is the same for `public` or `protected`. – Fareanor Mar 09 '20 at 15:34
  • Sorry @both of you, I don't get it. I checked the link. I usually use e.g. this `static bool const debugging = true;` but I have problems with the vector, it isn't working as expected...? – alve89 Mar 09 '20 at 15:36
  • @MaximEgorushkin: No, not exactly, because there are more levels (11,21,31,42,...) without a minimum and a maximum. – alve89 Mar 09 '20 at 15:37
  • @alve89 The point is that `static` members usually cannot be initialized in the class declaration. You should initialize it in the implementation file. But anyway, the answer provided by NathanOliver better suits your needs :) – Fareanor Mar 09 '20 at 15:39

3 Answers3

5

The easiest thing to do is change your class into a namespace

namespace configuration {
  const std::vector<int> debugLevels = {1,2,3,4,5};
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I was about to use my hammer, but i think this answer justifies to not close it as duplicate. – 463035818_is_not_an_ai Mar 09 '20 at 15:33
  • @alve89 Your welcome. My rule of thumb is if you need a collection of variables, but you do not need one collection per an instance, then just put them in a namespace. – NathanOliver Mar 09 '20 at 15:47
  • I didn't know namespaces in C++ yet. I know it from PHP but there a namespace is working differently compared with C++, so I would never have thought of using a namespace for my problem. – alve89 Mar 09 '20 at 15:58
  • I would add that using std::vector for a const array is a waste. Use std::array (since you know the size at compile time, thus, you can use the simplest solution for it). – Kerek Mar 09 '20 at 18:15
1

std::vector doesn't seem to be warranted here. A min/max would do:

class configuration {
  public:
  static constexpr int debugLevelMin = 1;
  static constexpr int debugLevelMax = 5;
};

void setup() {
  for(int i=debugLevelMin; i<=debugLevelMax; i++ {
    // do some stuff here...
  }
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
1

In case that you need to use something similar, notice that the initialization should occur outside of the class definition (more specifically, in the corresponding .cpp file), meaning:

config.hpp:

class configuration 
{
  public:
  static const std::array<int, 5> debugLevels;
};

config.cpp:

const std::array<int, 5> configuration::debugLevels = {1,2,3,4,5};

As I have written above, probably the usage of std::array is better in this case, and I agree that if there is no need for a class functionality, you should use namespace instead.

Kerek
  • 1,106
  • 1
  • 8
  • 18