简体   繁体   English

错误:函数不是“类”的静态数据成员 - C++

[英]ERROR: Function is not a static data member of “Class” - C++

I'm very sorry if this has already been answered somewhere, but after hours of searching, I couldn't find or understand anything.如果已经在某处回答了这个问题,我很抱歉,但经过数小时的搜索,我找不到或理解任何内容。

Being quite new to OOP, I'm exercising myself with classes by trying to create a class where I pre-defined a 2 dimensions character matrix.作为 OOP 的新手,我通过尝试创建一个类来锻炼自己,我在其中预定义了一个 2 维字符矩阵。 I keep getting the following error:我不断收到以下错误:

error: 'char Matrix2d::keyss [4][4]' is not a static data member of 'class Matrix2d' char Matrix2d::keyss [ROWS][COLS] =错误:'char Matrix2d::keyss [4][4]' 不是 'class Matrix2d' char Matrix2d::keyss [ROWS][COLS] = 的静态数据成员

My header is the following:我的标题如下:

const int ROWS = 4;
const int COLS = 4;

class Matrix2d
{
  public:

   char keys [ROWS][COLS];

  private:

};

And My .cpp being this:我的 .cpp 是这样的:

char Matrix2d::keys [ROWS][COLS] =

{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

This is a simple code made to run with an arduino keypad.这是一个用 arduino 键盘运行的简单代码。

Thank you in advance for any help and hope I gave enough information as it's my first time posting here.预先感谢您的任何帮助,并希望我提供足够的信息,因为这是我第一次在这里发帖。

If the class is defined like that :如果类是这样定义的:

const int ROWS = 4;
const int COLS = 4;

class Matrix2d
{
  public:

   char keys [ROWS][COLS];

  private:

};

that means keys is an attributes of the instance of Matrix2d , but the form这意味着Matrix2d实例的属性,但形式

char Matrix2d::keys [ROWS][COLS] =

{
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

defines and initializes an attribute of the class Matrix2d , this is incompatible定义并初始化类Matrix2d的属性,这是不兼容的


If you want an attribute of the class (a 'static' one) do如果您想要类的属性(“静态”属性),请执行

const int ROWS = 4;
const int COLS = 4;

class Matrix2d
{
  public:
     static char keys [ROWS][COLS];    
};

char Matrix2d::keys [ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

If you want an attribute of instances having that default value do如果您想要具有该默认值的实例属性,请执行

class Matrix2d
{
  public:
     char keys [ROWS][COLS] = {
       {'1','2','3','A'},
       {'4','5','6','B'},
       {'7','8','9','C'},
       {'*','0','#','D'}
     };
};

In both case I encourage you to change the visibility在这两种情况下,我都鼓励您更改可见性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM