0

I only want to declare an array of objects of class control with size of 5. The contents of the specific objects will be filled afterwards.

class control {
  public:
    control(char* controlName) {
    name = controlName;
  }

  private:
  char* name;
};

void setup() {
  control humidityControl("humidityControl");
  // Problem: Declare an array controlArray with the size of 5 and the name "controlArray"
  control controlArray[5]("controlArray"); // Error: no matching function for call to 'control::control()'
  control controlArray("controlArray")[5]; // Error: expected ',' or ';' before '[' token
}


void loop() {

}

I'm using C++ on my Arduino. I'd appreciate every tipp how to fix this. Thanks!

alve89
  • 971
  • 1
  • 9
  • 31
  • Also relevant: [How do I declare an array of objects whose class has no default constructor?](https://stackoverflow.com/q/2343558/580083). – Daniel Langr Feb 17 '20 at 08:34
  • No, because as I I know there's no library `vector` for Arduino. – alve89 Feb 17 '20 at 08:37
  • 1
    Have you seen this question: [Vectors in Arduino](https://stackoverflow.com/q/9986591/580083)? – Daniel Langr Feb 17 '20 at 08:39
  • BTW, are you aware that by assigning pointers you don't copy data they are pointing to? – Daniel Langr Feb 17 '20 at 08:41
  • I haven't seen that question, that's great, thanks! Just for information: Is there a way to fix this without this library? And: Actually I wanted to copy the name with class control { public: control(char controlName[10]) { name[10] = controlName; // This works name = controlName; // This doesn't -> why? } private: char name[10]; };`` – alve89 Feb 17 '20 at 08:52
  • Yes, in the second question I linked, see the second answer. – Daniel Langr Feb 17 '20 at 08:58
  • Because you cannot copy c-strings just by assigning pointers/arrays. You need to use `strcpy` or similar functions. – Daniel Langr Feb 17 '20 at 09:01
  • Well, but that only works if I declare and define it in one step AND I know the contents when I define it. But I don't know that yet, it will be filled during runtime. Or did I misunderstood the answer...? – alve89 Feb 17 '20 at 09:01
  • 1
    Then, generally, you can hardly avoid dynamic memory allocations. If you don't know the numbers in advance, there is basically no other way. – Daniel Langr Feb 17 '20 at 09:04

1 Answers1

0

Your problem is that you're mixing the declaration of an array (the square brackets) with a call to the constructor. This SO answer clarifies it: https://stackoverflow.com/a/1598409/2881667.

What you want to do is this:

void setup() {
  control controlArray[5] = {"controlArray", "controlArray", "controlArray", "controlArray", "controlArray"};
}

Notice how you have to include the argument to the constructor 5 times. To avoid that, you have two options:

  1. Use a default constructor:
control(char* controlName = "controlArray");

Might work for you, but only if you plan on initializing all arrays with the same value.

  1. If using gcc, you can use its extension to initialize it like this:
control controlArray[5] = {[0 ... 4] = "controlArray"};

But this will only work with gcc, so not so good for portability.

Also, unrelated to your question - I think you probably meant to copy the string, and not store the pointer in your class, so you should use something like strncpy in the constructor, instead of just assigning to name, and you'll have to preallocate storage for name in that case (either declare it as a char array, like char name[255];, allocate space for it in the constructor, or use std::string to make it all easier).

Mr. 47
  • 76
  • 4
  • Unfortunately your suggestions don't work for me (the name differ). About your string suggestion: I tried that `class control { public: control(char controlName[10]) { name = controlName; // Throws error: incompatible types in assignment of 'char*' to 'char [10]' } private: char name[10]; //char* name; }; void setup() { control humidityControl("humidityControl"); } void loop() { }` but it doesn't work... I'd rather copy the value than pointing to it. But why doesn't that work? – alve89 Feb 17 '20 at 09:07
  • The source (the argument `controlName`) should remain a `char *`. – Mr. 47 Feb 17 '20 at 12:00
  • Sorry, I don't get it - I tried the `*` at all possible places but it doesn't work... – alve89 Feb 17 '20 at 13:42