首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino错误(C++):非静态数据成员的无效使用

Arduino错误(C++):非静态数据成员的无效使用
EN

Stack Overflow用户
提问于 2016-03-23 05:41:35
回答 2查看 2K关注 0票数 3

我正在制作一个可伸缩的Arduino库,但是我收到了编译器-error:invalid use of non-static data member

我的代码: LedCube.h:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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。我使用这些变量来声明一个循环。

我得到的错误是:

代码语言:javascript
复制
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.

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-23 09:19:27

代码语言:javascript
复制
class LedCube {
  private:
  int _x, _y, _z;
  byte _lPins[_y];
  byte _cPins[_z][_x];

上面的代码没有任何意义,Arduino或not。(请记住,Arduino使用C++)。

您正在尝试定义数组_lPins_cPins,它们的长度都是未初始化的_x, _y, _z。类必须有固定的大小,所以当您实例化它时,编译器知道要分配多少内存(在调用构造函数之前)。它如何为未知大小的数组分配内存?

(编辑以添加)

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

我很想知道为什么你会接受这样的回答:“你的代码:.是错的。”但要对我发火。接受的答案没有解决方案代码,只是一些指导方针。

您的代码,而不是类定义中的代码,也犯了同样的错误:

代码语言:javascript
复制
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,它也是一个类变量。

代码语言:javascript
复制
LedCube::LedCube(int x, int y, int z, byte lPins[], byte cPins[][_x]) {

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

代码语言:javascript
复制
 _lPins = lPins;
 _cPins = cPins;

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

代码语言:javascript
复制
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函数显示数据已被正确保留。

代码语言:javascript
复制
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() 
{

}

输出:

代码语言:javascript
复制
lPins:
5
6
7
cPins:
z = 0
1
2
3
4
z = 1
8
9
10
11
票数 3
EN

Stack Overflow用户

发布于 2016-03-23 06:07:06

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

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

解决这个问题的“正确”方法是使用std::vector,但我认为您的平台上不存在它,不过您应该检查一下。IIRC,唯一可用的库是来自avr的标准C的子集。

您将不得不动态地分配内存,因为您自己需要它。同样,IIRC、newdelete在您的平台上也没有提供(但同样,您必须检查一下),所以您必须使用mallocfree,并以旧的C方式进行操作。关于如何在网上做到这一点,有很多可用的资源。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36170616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档