Reputation: 131
I have
struct Voice {
static byte mode;
//Some other stuff
};
byte Voice::mode{}; //Static member defined outside of struct declaration
and
struct DataPacket {
DataPacket() : sequencer{}, voice{}, tempdata{} {};
Sequencer sequencer;
Voice voice[4];
Tempdata tempdata;
};
I want to assign one of the voice struct members in the array with a variable value like this:
DataPacket assignVoiceValues(const InputPacket &inputPacket,
DataPacket &dataPacket,
const byte &voiceNumber) {
dataPacket.voice[voiceNumber].mode = (byte) inputPacket.finalPotvalue[0];
//Other code
}
Even though this compiles, when i test the code all of the four structs members mode
in the voice[]
array are simultaneously assigned with inputPacket.finalPotvalue[0]
. There is no code for assigning values to dataPacket.voice[voiceNumber].mode
elsewhere that could possibly interfere.
I have no idea why this is happening. What am i missing, and what is the proper syntax for making it work as intended?
(I know vectors generally are recommended over arrays, but the code is intended for an arduino board with limited memory).
Upvotes: 1
Views: 96
Reputation: 12147
Since you declare variable "mode" static it is bound to your class (which is single), not to instances of your class (you can create a lot of instances).
To fix the problem just remove static from description of your variable
https://en.cppreference.com/w/cpp/language/static
Upvotes: 0
Reputation: 81926
You have define mode as static byte mode;
... So there's space allocated for exactly one of them in the entire program.
Perhaps mode
should not be marked as static
?
Upvotes: 1