global variables in a library

i just want tu use an Array and interger variabl all over in my cpp file.
i’d like to use it my serialaevent() function and in other function of the class, so that the Array can be modified whenever the SerialEvent() funtion is called.
below the Header and cpp files.

h file

#ifndef RFID_h
#define RFID_h
#include "Arduino.h"

class RFID{      
friend void serialEvent();

private:
       void Senden(int Nutzbytesanzahl,byte NTZBYTE1, byte NTZBYTE2);            
       byte* Empfangen(int laenger_der_gelesene_bytes);
       int Fehlerbehandlung(int  laenger_der_gelesene_bytes);
       uint8_t Send_Recievepin;
       
public:
     int Counter=0;
       static byte tel[20]; 
     RFID( uint8_t Send_Recievepin);
     int Get_Sw_Version();
     int Get_Hw_Version();
     int Transmitter_On();
     int Get_System_Infos();
     int Parameter();
     byte* Read_Block();         
};
#endif

cpp file

byte* RFID::Read_Block()
{
RFID_Serialport.begin(115200);
Senden(RB_Nutzbytesanzahl,RB_Nutzbyte1, RB_Nutzbyte2);
digitalWrite(Send_Recievepin, LOW);
if (counter<=1)
return tel;

else if (counter>=11)
return tel;
}
       
void serialEvent()
{
if (Serial.available()>0){
 tel[counter]= Serial.read();

if (tel[counter]==0x77){
   counter ++;
   return tel[counter];
   }
else if (tel[counter]==0xAA){
    tel[counter]= Serial.read();
     delay(1);
     counter ++;
   }
}  
}

thank you in a anticipation

thank you in a anticipation

What are you anticipating? You did not ask a question.

You also failed to read the stickies at the top of the forum, where it was explained how to post code, so that it doesn't resemble the crap you posted.

Do NOT get cute trying to color code.

i formulated my wish on the top.
i would be grateful if you could answer if ou can and not offending me. :slight_smile:

it's now correct. can someone help me out

i just want tu use an Array and interger variabl all over in my cpp file.

Which "Array"? Which "interger variabl"?

When the serialEvent() function is called, which instance of the RFID class' tel and counter variables are to be manipulated?

Why does serialEvent() expect tel to be an array, while the RFID class defines it to be a scalar?

the Array tel and the integer variable Counter have to be manipuled.

i just rectified it , tel schould be an array

The array tel, and the variable counter, are members of the class. To manipulate them, you need either an instance of the class that you use in the serialEvent() method, or you need to make them global variables. You should NOT do that in the header file. You should NOT do that in the source file, either.

And, of course, if they truly are global, then it is pointless to have that method return tel.

so where should i make them global.
can u Show me how you would do it based on my Code
thx

so where should i make them global.

In your sketch.

can u Show me how you would do it based on my Code

No, because I wouldn't do it. I KNOW that one of the principles of object oriented programming is encapsulation, where private data is private.

I also understand that you are writing a class to encapsulate some data, but you do not understand that there in NOTHING in your class to prevent defining multiple instances of the class. When you do that, exactly which instance of the class is supposed to get the serial data?

If you insist that YOU will NEVER create more than one instance, then either there is no point in having a class, or you need to define the class as a singleton.

Before you continue trying to defeat the principle of encapsulation, you REALLY should explain WHY you (think you) need to do that.

Global variables are normally declared in your sketch - the .ino file. Then they're available to all functions, but not sure if they can be used by classes tbh. I've never seen the need for it, on the contrary: classes should not mess around with variables that don't belong to them.

If you want to have a variable that's available to all functions in your .cpp file, you declare it in the .h file.

but not sure if they can be used by classes tbh.

Sure they can. All that is needed is an extern statement in the class header or source, as appropriate, stating that the variable/array is declared outside of this compilation unit. The linker will than take care of matching up the name used in this compilation unit to the address assigned to that name in the declaring compilation unit.

C++ really doesn't go out of its way providing ways to shoot yourself in the foot :slight_smile:

wvmarle:
C++ really doesn't go out of its way providing ways to shoot yourself in the foot :slight_smile:

Yes, it most certainly does.

You could use "extern" to declare/access a global variable.

@pispa is currently away from the forum, for a short reflection on why it is a Bad Ideatm to post the same question three times in different parts of the forum.