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!

9
  • Also relevant: How do I declare an array of objects whose class has no default constructor?. Commented Feb 17, 2020 at 8:34
  • No, because as I I know there's no library vector for Arduino. Commented Feb 17, 2020 at 8:37
  • 1
    Have you seen this question: Vectors in Arduino? Commented Feb 17, 2020 at 8:39
  • BTW, are you aware that by assigning pointers you don't copy data they are pointing to? Commented Feb 17, 2020 at 8:41
  • 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. Commented Feb 17, 2020 at 9:04

1 Answer 1

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).

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

3 Comments

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?
The source (the argument controlName) should remain a char *.
Sorry, I don't get it - I tried the * at all possible places but it doesn't work...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.