I am making a scalable Arduino library but I receive the compiler-error: invalid use of non-static data member.
My Code: LedCube.h:
#ifndef LedCube_h
#define LedCube_h
#include "Arduino.h"
class LedCube {
private:
int _x, _y, _z;
byte _lPins[_y];
byte _cPins[_z][_x];
public:
LedCube(int x, int y, int z, byte *lPins, byte (*cPins)[_x]);
void displayFrame(bool frame[][_x][_z]);
void displayLayer(int i, bool frame[][_x][_z]);
};
#endif
Ledcube.ino(cpp):
#include "Arduino.h"
#include "LedCube.h"
int _x, _y, _z;
//bool frame[y][z][x] = {0};
byte _lPins[_y];
byte _cPins[_z][_x];
LedCube::LedCube(int x, int y, int z, byte lPins[], byte cPins[][_x]) {
_x = x;
_y = y;
_z = z;
_lPins = lPins;
_cPins = cPins;
}
void LedCube::displayFrame(bool frame[_y][_x][_z]) {
int i;
for(i=0;i<_y;i++) {
displayLayer(i, frame);
pinMode(_lPins[i], OUTPUT);
delay(1);
pinMode(_lPins[i], INPUT);
}
}
void LedCube::displayLayer(int i, bool frame[_y][_x][_z]) {
int j,k;
for(j=0;j<_z;j++) {
for(k=0;k<_x;k++) {
if(frame[i][j][k]) {
digitalWrite(_cPins[j][k], HIGH);
}
else {
digitalWrite(_cPins[j][k], LOW);
}
}
}
}
I want to accept the variables x, y and z and set these to _x, _y and _z in the constructor, and hence do not want to set the variables static.
I am using these variables to declare a loop.
The error I get is:
Arduino: 1.6.5 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_y'
int _x, _y, _z;
^
LedCube.h:13: error: from this location
byte _lPins[_y];
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_z'
int _x, _y, _z;
^
LedCube.h:14: error: from this location
byte _cPins[_z][_x];
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube.h:14: error: from this location
byte _cPins[_z][_x];
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube.h:16: error: from this location
LedCube(int x, int y, int z, byte *lPins, byte (*cPins)[_x]);
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube.h:17: error: from this location
void displayFrame(bool frame[][_x][_z]);
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_z'
int _x, _y, _z;
^
LedCube.h:17: error: from this location
void displayFrame(bool frame[][_x][_z]);
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube.h:18: error: from this location
void displayLayer(int i, bool frame[][_x][_z]);
^
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_z'
int _x, _y, _z;
^
LedCube.h:18: error: from this location
void displayLayer(int i, bool frame[][_x][_z]);
^
LedCube:6: error: array bound is not an integer constant before ']' token
LedCube:7: error: array bound is not an integer constant before ']' token
LedCube:7: error: array bound is not an integer constant before ']' token
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube:9: error: from this location
LedCube.ino: In constructor 'LedCube::LedCube(...)':
LedCube:10: error: 'x' was not declared in this scope
LedCube:11: error: 'y' was not declared in this scope
LedCube:12: error: 'z' was not declared in this scope
LedCube:13: error: '_lPins' was not declared in this scope
LedCube:13: error: 'lPins' was not declared in this scope
LedCube:14: error: '_cPins' was not declared in this scope
LedCube:14: error: 'cPins' was not declared in this scope
In file included from LedCube.ino:2:0:
LedCube.h: At global scope:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_y'
int _x, _y, _z;
^
LedCube:17: error: from this location
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube:17: error: from this location
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_z'
int _x, _y, _z;
^
LedCube:17: error: from this location
LedCube.ino: In member function 'void LedCube::displayFrame(...)':
LedCube:20: error: 'frame' was not declared in this scope
LedCube:21: error: '_lPins' was not declared in this scope
In file included from LedCube.ino:2:0:
LedCube.h: At global scope:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_y'
int _x, _y, _z;
^
LedCube:27: error: from this location
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_x'
int _x, _y, _z;
^
LedCube:27: error: from this location
In file included from LedCube.ino:2:0:
LedCube.h:12: error: invalid use of non-static data member 'LedCube::_z'
int _x, _y, _z;
^
LedCube:27: error: from this location
LedCube.ino: In member function 'void LedCube::displayLayer(...)':
LedCube:31: error: 'frame' was not declared in this scope
LedCube:31: error: 'i' was not declared in this scope
LedCube:32: error: '_cPins' was not declared in this scope
LedCube:35: error: '_cPins' was not declared in this scope
invalid use of non-static data member 'LedCube::_y'
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
I just want to target this error and not the others in the output.