Class objects crushing each other

Hello All,
This is my first post on the Forum as i am new on Arduino programming.
I am having a problem on my sketch which i describe below:
I have created a Library, that i am using, i have 2 classes, ClassA and ClassB, however, when i create an instance of ClassA and then an Array with size 5 of elements of ClassB it seems that the ClassA element is completely overrided, or chrushed and if i do a getName it gives me random things:

Example:

ClassA obj1(1) // This creates the object one with a name and id.
ClassB array[5] // This create an array of ClassB elements.
obj1.getName(); //Gives me just completely nonsense result.

If i change the order and create the obj1 after the array then getName already works fine and i can see obj1 name.

Sorry i can’t explain myself better and thanks to whoever answers.
Thank you,
Jelson Gonçalves

How can you expect a sensible answer without providing the whole code?

SMH

Hi Sorry i was trying to resume everything to make it easier to read, please find the code below:

/*

** -------------------------------------------------------**

** Troubleshooting class objects – COM4**

** -------------------------------------------------------**

*/

// ------------- Libraries

#include <Radio_Antenna.h> // The library i created

#include <SPI.h>

#include <nRF24L01.h>

#include <printf.h>

#include <RF24.h>

#include <RF24_config.h>

// ------------- Object Creation

RF24 radio(7, 8); // CE, CS

// ------------- Identification

MobileAntenna mobile(1, “00001”, “11110”);

FixedAntenna antenna1(1);

void setup() {

** printf_begin();**

** Serial.begin(9600);**

** radio.begin();**

** radio.printDetails();**

** radio.enableAckPayload();**

}

void loop() {

Serial.println(“Fixed Antenna id is:” + antenna1.getAntennaId()); // expected result rfFix01

delay(2000);

Serial.println(“Mobile Antenna id is:” + mobile.getAntennaId()); // expected result rfMob01

delay(2000);

** }**

In this code, the only object that correctly shows his id is the one that is created at last, so i am assuming that it is crushing the first one. Please find also the c.h and .cpp codes below:

headers file:

class MobileAntenna
{
public:
MobileAntenna();
MobileAntenna(int antenna_Id, byte writing_Channel[6], byte reading_Channel[6] );
String getAntennaId();
byte* getAntennaReadingChannel();
byte* getAntennaWritingChannel();
private:
String antennaId;
byte antennaReadingChannel[6];
byte antennaWritingChannel[6];
};

class FixedAntenna
{
public:
FixedAntenna(int antenna_Id);
FixedAntenna(int antenna_Id, byte a[6], byte b[6]);
String getAntennaId();
private:
String antennaId;
byte antennaBroadcastChannel[6];
byte antennaBroadcastCallChannel[6];
};

CPP file:

FixedAntenna::FixedAntenna(int antenna_Id)
{

String ant_id;
if(antenna_Id > 9){
ant_id = String(antenna_Id);
} else{
ant_id = String(0) + String(antenna_Id);
}
antennaId = “rfFix” + ant_id;
byte a[6] = “00000”;
byte b[6] = “11111”;
antennaBroadcastChannel[6]=a[6];
antennaBroadcastCallChannel[6]=b[6];
}

String FixedAntenna::getAntennaId(){
return antennaId;
}

MobileAntenna::MobileAntenna(int antenna_Id, byte writing_Channel[6], byte reading_Channel[6])
{
String ant_id;
if(antenna_Id > 9){
ant_id = String(antenna_Id);
} else{
ant_id = “0” + String(antenna_Id);
}
antennaId = “rfMob” + ant_id;
antennaReadingChannel[6]=reading_Channel[6];
antennaWritingChannel[6]=writing_Channel[6];
}

String MobileAntenna::getAntennaId(){
return antennaId;
}

Thanks in advance

Overwrite a nonexistent element with arbitrary memory (content of a nonexistent element).

Thank you for your answer, so how could i do it?
Does this means that i should not have arrays on my constructors?

Thanks

I have no idea what you want to accomplish with those statements.

Arrays can be copied by memcpy or similar standard C functions.

Basically i have a class called FixedAntenna and one of the class property is a byte array, that should be passed in the constructor.
So what i want to do is to copy the array passed on the constructor to the element property.

Thanks

You are free to do so.

A simple loop could work, memcpy was already mentioned.
Arrays are passed as pointers with no way to access their original lengths.
If the arrays are to have a variable length, the number of elements needs to be passed also.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.