Arduino Error(C ++):无效使用非静态数据成员

时间:2016-03-23 05:41:35

标签: c++ arrays compiler-errors arduino

我正在制作一个可扩展的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);
      }
    }
  }
}

我想接受变量xyz,并在构造函数中将它们设置为_x_y_z ,因此不想设置变量static。 我正在使用这些变量来声明循环。

我得到的错误是:

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.

我只想针对此错误而不是输出中的其他错误。

2 个答案:

答案 0 :(得分:3)

class LedCube {
  private:
  int _x, _y, _z;
  byte _lPins[_y];
  byte _cPins[_z][_x];

以上代码没有任何意义,Arduino与否。 (请记住Arduino使用C ++)。

您正在尝试定义长度为_lPins且未初始化的数组_cPins_x, _y, _z。类必须具有固定的大小,以便在实例化时,编译器知道分配它的内存量(在调用构造函数之前)。如何为未知大小的数组分配内存?

(已编辑添加)

  

我认为StackOverflow有一个建设性的答案政策。如果你认为我做错了,请给出一个解决方案(这就是我在这里的原因)。

我很有兴趣知道为什么你会接受一个答案,说明&#34;你的代码:......是错的。&#34;但我的冒犯。接受的答案没有发布解决方案代码,只是一些指导原则。

你的这段代码,不在类定义中,也会犯同样的错误:

int _x, _y, _z;
//bool frame[y][z][x] = {0};
byte _lPins[_y];
byte _cPins[_z][_x];

这也会产生错误。您不能声明这样的静态数组,其边界为x, _y, _z,其中x, _y, _z不是常量。当您更改x, _y, _z时,阵列将来不会重新定义其长度。

在构造函数中,您将作为参数传递,名称_x也是类变量。

LedCube::LedCube(int x, int y, int z, byte lPins[], byte cPins[][_x]) {

C ++不允许您分配这样的数组:

 _lPins = lPins;
 _cPins = cPins;

你有很多错误,而不仅仅是一个,例如

LedCube:20: error: 'frame' was not declared in this scope
LedCube:21: error: '_lPins' was not declared in this scope

你真的应该经历并清理它们。

  

同样,您的平台上也未提供IIRC,new和delete

实际上,Arduino提供newdelete,以及mallocfree

我试图建设性,但没有一线修复。需要返工,很遗憾地告诉你。你可能想做一些C ++教程。您正在编写的代码(虽然我可以看到您要执行的操作)是希望该语言以某种方式工作,当它只是没有。

可能的实施

下面是根据提供的数组大小在构造函数中分配引脚结构的可能方法。我不认为这是一个很好的实现,但至少它是有效的。 showPins函数显示数据已正确保留。

class LedCube {
  private:
    const int x_, y_, z_;
    byte * lPins_;
    byte * cPins_;
  public:
    LedCube(const int x, const int y, const int z, byte *lPins, byte *cPins);  // constructor

    void showPins () const;
};

byte lPins [3] = { 5, 6, 7 };
byte cPins [2] [4] = {
    { 1, 2, 3, 4 },    // 0
    { 8, 9, 10, 11 },  // 1
};

LedCube::LedCube (const int x, const int y, const int z, byte *lPins, byte *cPins)
    : x_ (x), y_ (y), z_ (z)
  {
  lPins_ = new byte [y];
  cPins_ = new byte [x * z];
  if (lPins_ == NULL || cPins_ == NULL)
    exit (1);
  memcpy (lPins_, lPins, sizeof (byte) * y);
  memcpy (cPins_, cPins, sizeof (byte) * x * z);
  }

void LedCube::showPins () const
  {
  Serial.println (F("lPins:"));
  for (int i = 0; i < y_; i++)
    Serial.println (lPins_ [i]);
  Serial.println (F("cPins:"));
  for (int j = 0; j < x_; j++)
    {
    Serial.print (F("z = "));
    Serial.println (j);
    for (int k = 0; k < z_; k++)
      Serial.println (cPins_ [j * z_ + k]);
    } 
  }

LedCube foo (2, 3, 4, (byte *) lPins, (byte *) cPins);

void setup() 
{
  Serial.begin (115200);
  Serial.println ();
  foo.showPins ();
}

void loop() 
{

}

输出:

lPins:
5
6
7
cPins:
z = 0
1
2
3
4
z = 1
8
9
10
11

答案 1 :(得分:2)

您的代码:byte _lPins[_y]; byte _cPins[_z][_x];错了。 C ++不支持可变长度数组,这意味着_y_x_z必须是常量表达式。

请记住,对象的大小在编译时必须是常量,并且您不会再次出现该错误。

&#34;正确&#34;解决此问题的方法是使用std::vector,但我相信它在您的平台上不存在,但您应该检查。 IIRC,唯一可用的库是来自avr-libc的标准C的子集。

您必须自己动态分配内存。同样,IIRC,newdelete也未在您的平台上提供(但同样,您必须检查我),因此您必须使用{{ 1}}和malloc并以旧C方式做事。有关于如何在线进行此操作的大量资源。