Hello!
So far, I've been making arrays and placing data outside of class, like here for example;
const byte ARRAY_SIZE = 3;
const byte ARRAY_SIZE2 = 9;
byte dataArray[ARRAY_SIZE];
byte dataArray2[ARRAY_SIZE2];
char arrayData[20];
void setup() {
for (byte i = 0; i < sizeof(dataArray); i++) {
//Just filling in array with some data
dataArray[i] = i;
}
for (byte i = 0; i < sizeof(dataArray2); i++) {
//Just filling in array with some data
dataArray2[i] = i;
}
Serial.begin(115200);
}
void loop() {
delay(1000);
sprintf(arrayData, "%s%d", "Size of array1: ", sizeof(dataArray));
Serial.println(arrayData);
Serial.print(F("Data in array1: "));
for (byte i = 0; i < sizeof(dataArray); i++) {
Serial.print(dataArray[i]);
}
Serial.println('\n');
sprintf(arrayData, "%s%d", "Size of array2: ", sizeof(dataArray2));
Serial.println(arrayData);
Serial.print(F("Data in array2: "));
for (byte i = 0; i < sizeof(dataArray2); i++) {
Serial.print(dataArray2[i]);
}
Serial.println('\n');
}
But when I try to make class which object array size can be adjusted when creating object, I'm not having much luck.
So far, I've found methods of using, and also tried. Codes are attached into this post what Iv'e tried.
-Template
-#Define
-Pointer
I've never used templates, so they are bit confusing at this point; yet using them Is just fine.
Thinking about #define, I haven't found method of using #define with many objects with variable values. Like here, #define works with two arrays, but passing different #define values into class escapes me;
#define ARRAY_SIZE 5
#define ARRAY_SIZE2 8
byte dataArray[ARRAY_SIZE];
byte dataArray2[ARRAY_SIZE2];
char arrayData[20];
void setup() {
for (byte i = 0; i < sizeof(dataArray); i++) {
//Just filling in array with some data
dataArray[i] = i;
}
for (byte i = 0; i < sizeof(dataArray2); i++) {
//Just filling in array with some data
dataArray2[i] = i;
}
Serial.begin(115200);
}
void loop() {
delay(1000);
sprintf(arrayData, "%s%d", "Size of array1: ", sizeof(dataArray));
Serial.println(arrayData);
Serial.print(F("Data in array1: "));
for (byte i = 0; i < sizeof(dataArray); i++) {
Serial.print(dataArray[i]);
}
Serial.println('\n');
sprintf(arrayData, "%s%d", "Size of array2: ", sizeof(dataArray2));
Serial.println(arrayData);
Serial.print(F("Data in array2: "));
for (byte i = 0; i < sizeof(dataArray2); i++) {
Serial.print(dataArray2[i]);
}
Serial.println('\n');
}
Now then, progress of making actual class is now this. This is using pointers, but it escapes me why object doesn't get correct size & data passed.
class arrayInClass {
public:
arrayInClass::arrayInClass(byte* dataArray, byte arraySize, char* arrayName) {
_arraySize = arraySize;
_dataArray = dataArray;
_arrayName = arrayName;
}
byte getArraySize() {
return sizeof(_dataArray);
}
char* getArrayName() {
return _arrayName;
}
byte getArrayValue(byte arrayIndex) {
return _dataArray[arrayIndex];
}
private:
byte _arraySize;
byte* _dataArray;
char* _arrayName;
};
const byte ARRAY_SIZE = 10;
byte dataArray[ARRAY_SIZE];
arrayInClass newArray(dataArray, ARRAY_SIZE, "newArray");
char objectData[100];
void setup() {
for (byte i = 0; i < ARRAY_SIZE; i++) {
//Just filling in array with some data
dataArray[i] = i;
}
Serial.begin(115200);
}
void loop() {
delay(1000);
//First reading what's going in;
sprintf(objectData, "%s%s", "Object name: ", newArray.getArrayName());
Serial.println(objectData);
sprintf(objectData, "%s%d", "Size of array going in: ", sizeof(dataArray));
Serial.println(objectData);
Serial.print(F("Data of array going in: "));
for (byte i = 0; i < sizeof(dataArray); i++) {
Serial.print(dataArray[i]);
}
Serial.println('\n');
//Next reading what actually is in object;
sprintf(objectData, "%s%d", "Size of array in object: ", newArray.getArraySize());
Serial.println(objectData);
Serial.print(F("Data of array in object: "));
for (byte i = 0; i < newArray.getArraySize(); i++) {
Serial.print(newArray.getArrayValue(i));
}
Serial.println('\n');
}
Serial output is as following;
Object name: newArray
Size of array going in: 10
Data of array going in: 0123456789
Size of array in object: 2
Data of array in object: 01
Here is my attempt of using #define, but it doens't like it
class arrayInClass {
public:
arrayInClass::arrayInClass(byte* dataArray, byte arraySize, char* arrayName) {
#define _arraySize arraySize
_dataArray = dataArray;
_arrayName = arrayName;
}
byte getArraySize() {
return sizeof(_dataArray);
}
char* getArrayName() {
return _arrayName;
}
byte getArrayValue(byte arrayIndex) {
return _dataArray[arrayIndex];
}
private:
#define _arraySize 3
byte _dataArray[_arraySize];
char* _arrayName;
};
const byte ARRAY_SIZE = 10;
byte dataArray[ARRAY_SIZE];
arrayInClass newArray(dataArray, ARRAY_SIZE, "newArray");
char objectData[100];
void setup() {
for (byte i = 0; i < ARRAY_SIZE; i++) {
//Just filling in array with some data
dataArray[i] = i;
}
Serial.begin(115200);
}
void loop() {
delay(1000);
//First reading what's going in;
sprintf(objectData, "%s%s", "Object name: ", newArray.getArrayName());
Serial.println(objectData);
sprintf(objectData, "%s%d", "Size of array going in: ", sizeof(dataArray));
Serial.println(objectData);
Serial.print(F("Data of array going in: "));
for (byte i = 0; i < sizeof(dataArray); i++) {
Serial.print(dataArray[i]);
}
Serial.println('\n');
//Next reading what actually is in object;
sprintf(objectData, "%s%d", "Size of array in object: ", newArray.getArraySize());
Serial.println(objectData);
Serial.print(F("Data of array in object: "));
for (byte i = 0; i < newArray.getArraySize(); i++) {
Serial.print(newArray.getArrayValue(i));
}
Serial.println('\n');
}
It gives following error, which I don't understand. I didn't touch array datatype at least on purpose
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassDefine\arrayInClassDefine.ino: In constructor 'arrayInClass::arrayInClass(byte*, byte, char*)':
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassDefine\arrayInClassDefine.ino:6:18: error: incompatible types in assignment of 'byte* {aka unsigned char*}' to 'byte [3] {aka unsigned char [3]}'
}
^
exit status 1
Compilation error: incompatible types in assignment of 'byte* {aka unsigned char*}' to 'byte [3] {aka unsigned char [3]}'
And finally, with template, which is new beast for me;
template<byte arraySize>
class arrayInClass {
public:
arrayInClass::arrayInClass(byte* dataArray, byte arraySize, char* arrayName) {
_arraySize = arraySize;
_dataArray = dataArray;
_arrayName = arrayName;
}
byte getArraySize() {
return sizeof(_dataArray);
}
char* getArrayName() {
return _arrayName;
}
byte getArrayValue(byte arrayIndex) {
return _dataArray[arrayIndex];
}
private:
byte _arraySize;
byte _dataArray[_arraySize];
char* _arrayName;
};
const byte ARRAY_SIZE = 10;
byte dataArray[ARRAY_SIZE];
arrayInClass<ARRAY_SIZE> newArray(dataArray, ARRAY_SIZE, "newArray");
char objectData[100];
void setup() {
for (byte i = 0; i < ARRAY_SIZE; i++) {
//Just filling in array with some data
dataArray[i] = i;
}
Serial.begin(115200);
}
void loop() {
delay(1000);
//First reading what's going in;
sprintf(objectData, "%s%s", "Object name: ", newArray.getArrayName());
Serial.println(objectData);
sprintf(objectData, "%s%d", "Size of array going in: ", sizeof(dataArray));
Serial.println(objectData);
Serial.print(F("Data of array going in: "));
for (byte i = 0; i < sizeof(dataArray); i++) {
Serial.print(dataArray[i]);
}
Serial.println('\n');
//Next reading what actually is in object;
sprintf(objectData, "%s%d", "Size of array in object: ", newArray.getArraySize());
Serial.println(objectData);
Serial.print(F("Data of array in object: "));
for (byte i = 0; i < newArray.getArraySize(); i++) {
Serial.print(newArray.getArrayValue(i));
}
Serial.println('\n');
}
Gives even bigger error
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:4:57: error: declaration of 'const byte arraySize' shadows template parameter
arrayInClass::arrayInClass(byte* dataArray, byte arraySize, char* arrayName) {
^~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:1:10: note: template parameter 'arraySize' declared here
template<byte arraySize>
^~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:4:83: error: explicit specialization of 'arrayInClass<arraySize>::arrayInClass(byte*, byte, char*)' must be introduced by 'template <>'
arrayInClass::arrayInClass(byte* dataArray, byte arraySize, char* arrayName) {
^
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:25:19: error: invalid use of non-static data member 'arrayInClass<arraySize>::_arraySize'
byte _dataArray[_arraySize];
^~~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:24:8: note: declared here
byte _arraySize;
^~~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino: In member function 'byte arrayInClass<arraySize>::getArraySize()':
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:12:19: error: '_dataArray' was not declared in this scope
return sizeof(_dataArray);
^~~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino: In member function 'byte arrayInClass<arraySize>::getArrayValue(byte)':
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:20:12: error: '_dataArray' was not declared in this scope
return _dataArray[arrayIndex];
^~~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino: At global scope:
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:32:68: error: no matching function for call to 'arrayInClass<10>::arrayInClass(byte [10], const byte&, const char [9])'
arrayInClass<ARRAY_SIZE> newArray(dataArray, ARRAY_SIZE, "newArray");
^
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate: arrayInClass<10>::arrayInClass()
class arrayInClass {
^~~~~~~~~~~~
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate expects 0 arguments, 3 provided
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate: constexpr arrayInClass<10>::arrayInClass(const arrayInClass<10>&)
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate expects 1 argument, 3 provided
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate: constexpr arrayInClass<10>::arrayInClass(arrayInClass<10>&&)
C:\Users\Atte\Desktop\arduino variable array size\arrayInClassTemplate\arrayInClassTemplate.ino:2:7: note: candidate expects 1 argument, 3 provided
exit status 1
Compilation error: declaration of 'const byte arraySize' shadows template parameter
I believe that's all info and what I've tried. I have feeling that it should be easy to do, but obviously method has eluded me. Also I might have used wrong terminology here and there.