-3

I have an C++ class with a bidimensional array of ints.

This member is setted by one method of the class.

class MyClass {
  private:
    int values[][4];

  public:
    MyClass();
    void setValues(int values[][4]);

if I do this:

MyClass::setValues(int values[][4]) {
  Serial.println(values[0][0]);
}

Everything works without erros. But if I do this:

MyClass::setValues(int values[][4]) {
  this->values = values;
}

I got invalid use of array with unspecified bounds error.

I need this bidimensional away to use on others method of this class.

How can I save this as a class member?

BrunoTS
  • 171
  • 2
  • 17
  • 2
    `std::vector` as a calculated 2-D vector, or `std::vector>` as a vector-of-vectors-of-ints. – Eljay Sep 22 '20 at 21:00
  • This is an unsized array, so you can't put it in a `class` like that. It needs a fixed size. Use `std::vector` if available, or `new[]` if not. – tadman Sep 22 '20 at 21:01
  • 2
    Forget C-style arrays exist. Exclusively use `std::array` or `std::vector` in their place. Live a happier life. – Jesper Juhl Sep 22 '20 at 21:04
  • 1
    [An example of the calculated 2D vector Eljay's talking about](https://stackoverflow.com/a/2076668/4581301). – user4581301 Sep 22 '20 at 21:04
  • std::vector> is not working... Do I need add some libary? – BrunoTS Sep 22 '20 at 22:07

1 Answers1

0

I solved this with Vector.h from ArduinoIDE libiries.

But, I don`t use a Vector of Vectors...

Frist of all, I created a struct

struct MyStruct {
  int a;
  int b;
  int c;
  unsigned long d;
  
  MyStruct(int a, int b, int c, unsigned long d) {
    this->a = a;
    this->b = b;
    this->c = c;
    this->d = d;
  }
};

Then, change my method to void set(Vector<MyStruct> steps); and call this like that:

  MyStruct steps[6] = {MyStruct(50, 0, 0, 100),
                      MyStruct(20, 0, 0, 100),
                      MyStruct(50, 0, 0, 100),
                      MyStruct(20, 0, 0, 100),
                      MyStruct(50, 0, 0, 100),
                      MyStruct(0, 0, 0, 500)
                     };
  Vector<MyStruct> vector(steps, 6);
  myClass.setSteps(vector);

It`s working now :D

BrunoTS
  • 171
  • 2
  • 17
  • 1
    Arduino is not a complete C++ compiler. Some responders don't realise this and suggest solutions like `std::vector` that work in regular C++ but not in Arduino. – john Sep 23 '20 at 11:27