【发布时间】:2016-07-10 07:09:09
【问题描述】:
我正在制作一个可扩展的 Arduino 库,但我收到编译器错误:invalid use of non-static data member。
我的代码: 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);
}
}
}
}
我想在构造函数中接受变量x、y和z并将它们设置为_x、_y和_z,因此不想设置变量@987654330 @。
我正在使用这些变量来声明一个循环。
我得到的错误是:
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.
我只想定位这个错误,而不是输出中的其他错误。
【问题讨论】:
-
你不能做
byte _lPins[_y];。但是您可以使用std::vector<byte>并在构造函数初始化列表中设置其长度。问题:Arduino 编译器不一定支持 C++ 标准库,如std::vector。解决方法:网上找这样的实现。 -
为什么有一个类成员
_lPins,还有一个全局变量_lPins?
标签: c++ arrays compiler-errors arduino