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