Sending an Array of Objects to a Constructor

HI,

I searched a lot around the web to try and find a solution but till now I had no luck. I need to send 2 arrays filled with objects to an other class so that I can call class methods from the other class. Here is a test code to explain my self better:

Main:

#include "Test2.h"

Test1 *arFun(Test1 vel1, Test1 vel2, Test1 vel3, Test1 vel4);
Test3 *arFun2(Test3 vel1, Test3 vel2, Test3 vel3, Test3 vel4);

Test1 obj1(5);
Test1 obj2(6);
Test1 obj3(7);
Test1 obj4(8);

Test3 abc1(1);
Test3 abc2(2);
Test3 abc3(3);
Test3 abc4(4);

Test2 col1(arFun(obj1, obj2, obj3, obj4), arFun2(abc1, abc2, abc3, abc4));

void setup() {
  Serial.begin(9600);
}

void loop(){
  col1.getArr();
  
}

Test1 *arFun(Test1 vel1, Test1 vel2, Test1 vel3, Test1 vel4){
  Test1 newAr[] = {vel1, vel2, vel3, vel4};
  return newAr;
}

Test3 *arFun2(Test3 vel1, Test3 vel2, Test3 vel3, Test3 vel4){
  Test3 newAr[] = {vel1, vel2, vel3, vel4};
  return newAr;
}

First Class cpp:

#include "Test1.h"

Test1::Test1(int val){
  value = val;
}

Test1::~Test1(){
}

void Test1::setVal(int val){
  value = val;
}

int Test1::getVal() const{
  return value;
}

First Class header:

#ifndef Test1_H
#define Test1_H

#include <Arduino.h>

// Class 1
class Test1{
  int value;

public:
  Test1(int);
  ~Test1();
  void setVal(int);
  int getVal() const;
};

#endif

Then there is an other class exectly like the previous with name "Test3" and after that an other class which is:
.cpp:

#include "Test2.h"

Test2::Test2(Test1 arr[], Test3 arr2[]){
  for ( int i=0; i<4; i++){
    myArr[i] = arr[i];
    myArr2[i] = arr2[i];
  }
}

Test2::~Test2(){}

void Test2::getArr() const{
  for (int i=0; i<4; i++){
    Serial.print(i);
    Serial.print(": ");
    Serial.print(myArr[i].getVal());
    Serial.print("       ");
    Serial.println(myArr2[i].getVal());
  }
}

and header:

#ifndef Test2_H
#define Test2_H

#include <Arduino.h>
#include "Test1.h"
#include "Test3.h"

// CLASS 2
class Test2{
private:
  Test1 myArr[];
  Test3 myArr2[];

public:
  Test2( Test1 arr[], Test3 arr2[]);
  ~Test2();
  void getArr() const;
};

#endif

Now with this code I am getting the output of the two arrays as the first one:
Serial Monitor:

0: 1 1
1: 2 2
2: 3 3
3: 4 4

any ideas how can I manage to solve this problem?
Thanks in advance

Test1 *arFun(Test1 vel1, Test1 vel2, Test1 vel3, Test1 vel4){
  Test1 newAr[] = {vel1, vel2, vel3, vel4};
  return newAr;
}

Test3 *arFun2(Test3 vel1, Test3 vel2, Test3 vel3, Test3 vel4){
  Test3 newAr[] = {vel1, vel2, vel3, vel4};
  return newAr;
}

newAr does not exist once the function returns. All you get is a pointer to somewhere on the stack.

Rather than declaring flat objects:

Test1 obj1(5);
Test1 obj2(6);
Test1 obj3(7);
Test1 obj4(8);

Test3 abc1(1);
Test3 abc2(2);
Test3 abc3(3);
Test3 abc4(4);

Create them as an array to begin with, then you can just pass in that:

Test1 objs[] = { 5,6,7,8 };
Test3 abcs[] = { 1,2,3,4 };

Test2 col1( objs, abcs );

Still got the same answer as before :frowning:

But thanks for helping...

Razgrizzz:
Still got the same answer as before :frowning:

But thanks for helping...

My post wasn't an answer to your question, it was showing you how to fix your broken code. Your functions arFun and arfun2 are not suitable for use, they are faulty.

No I meant:

#include "Test2.h"

Test1 objs[] = { 5,6,7,8};
Test3 abcs[] = {1,2,3,4};

Test2 col1(objs, abcs);

void setup() {
  Serial.begin(9600);
}

void loop(){
  col1.getArr();
  
}

I fixed it as you said but in the serial Monitor still got that strange result:

0: 1 1
1: 2 2
2: 3 3
3: 4 4

Maybe my problem now is in this part:

for ( int i=0; i<4; i++){
    myArr[i] = arr[i];
    myArr2[i] = arr2[i];
  }

where myArr is a private array in class Test2 delcared as -> Test1 myArr[];
and my array two is the same but of class Test3 -> Test3 myArr2[];

What do you think?

Here is an answer.
EDIT: you posted before I could finish, but yes you are close to the error.

Put the changes I mentioned in, then open up Test2

class Test2{
private:
  Test1 myArr[];
  Test3 myArr2[];

public:
  Test2( Test1 arr[], Test3 arr2[]);
  ~Test2();
  void getArr() const;
};

myArr & myArr2 have no size, they will both have the same memory location. When you copy the values in the constructor, you overwrite the first array with the second, which is why you print the same values. And by the way, the values you copy are placed into memory you do not own. If you create a global variable after it, you will notice that changing its value will change the array contents.

Arduino has little ram, do not make countless copies. So I would avoid correcting the code, and replace the arrays with pointers.
Use a pointer to the global array, then there is only one copy of the data.

Thanks a lot dude :slight_smile: fixed it as you said and worked just fine :smiley:
I created random empty objects

class Test2{
private:
  Test1 myArr[4];
  Test3 myArr2[4];

public:
  Test2( Test1 *arr, Test3 *arr2);
  ~Test2();
  void getArr() const;
};

then replaced the existing objects with pointers of the array we created:

Test2::Test2(Test1 *arr, Test3 *arr2){
  
  
  for ( int i=0; i<4; i++){
    myArr[i] = arr[i];
    myArr2[i] = arr2[i];
  }
 
}

Thanks again

That is one fix, however when I mentioned pointers I meant this:

class Test2{
private:
  Test1 *myArr;
  Test3 *myArr2;

public:
  Test2( Test1 *arr, Test3 *arr2);
  ~Test2();
  void getArr() const;
};
Test2::Test2(Test1 *arr, Test3 *arr2)  : myArr( arr ), myArr2( arr2 ){
  return;
}

Notice there is no loop and no duplicated data. Also the constructor would be best inlined in the struct definition.

:fearful: Holly crap lol Thats some advanced code.. I dindt know you could do that
Thanks again :smiley:

No worries, its just a constructor initialization list.

It is doing the same as the code below, it just happens before the code in the {} brackets runs.

Test2::Test2(Test1 *arr, Test3 *arr2){
  this->myArr = arr;
  this->myArr2 = arr2;
}