C ++ / Arduino Array in class

3

I have a problem in using an array of pointers, I need to create an array with pointers that refer to an integer value of each object of another class.

Example:

arrayDePonteiros[0] = objeto.int;

In case this array is inside a class and as it will be only a reference it will be static, so I can use this array to refer to the value of each object of the other class, which will be recorded in an eeprom in the future. When I read the value in eeprom I can use the pointer to pass the eeprom value to the object variable.

My current code is:

class Scenario {

public:
int byteInicial; // byte da eeprom
static int* link[6]; // atual array de ponteiros



Scenario(int byteI) // construtor da classe
{ 

byteInicial = byteI;
link[0] = &led1.fade;
}

In this case I get the error: undefined reference to 'Scenario :: link'. I've already tried using

Scenario::Scenario link [0] = &led1.fade;

But I get the error trying to use it anyway, either by printing on the serial or trying to write to eeprom. What would be the right way to do this?

    
asked by anonymous 16.08.2014 / 22:51

1 answer

2

This problem occurs because variables qualified as static in classes / structures must be defined outside the class and declared within it. So, somewhere in your cpp file that is associated with this class you must declare the array.

// Arquivo .h
class Scenario {
public:
    int byteInicial; // byte da eeprom
    static int* link[6]; // atual array de ponteiros

    Scenario(int byteI) // construtor da classe
    { 
        byteInicial = byteI;
        link[0] = &led1.fade;
    }
    // ...
};

// Arquivo .cpp
int * Scenario::link[6];

As for your code, do you really want to save only pointer to 1 object at a time as you are doing? In that case would not it be better to use a normal pointer instead of an array of pointers? These are questions that I think you should ask yourself.

    
17.08.2014 / 02:02