使用符号常量声明数组时遇到问题
Trouble declaring an array using symbolic constant
此代码无法编译:
#ifndef RemoteControl_h
#define RemoteControl_h
#include "Arduino.h"
class RemoteControl
{
public:
RemoteControl();
~RemoteControl();
static void prev_track();
static void next_track();
static void play_pause_track();
static void mute();
static void vol_up();
static void vol_down();
void respond(int code);
void add_code(int code, void (*func)());
private:
boolean active = true;
struct pair {
int _code;
void (*_func)();
};
const int max = 1000;
int database_length = 0;
pair database[max]; //This line doesn't compile unless I use a literal constant instead of "max"
};
#endif
但是,如果我将下面的部分放在 class 的构造函数中,它就可以正常工作。
const int max = 1000;
int database_length = 0;
pair database[max];
我是否不允许在 c++ 中的 class 中声明一个数组并使用虚拟常量作为长度?如果这有所不同,我正在 arduino 工作,但我希望我不理解 c++ 语言的某些东西,因为这是一个标准的 .h 文件。哦,问题不在于 .cpp 文件,因为我完全删除它并得到相同的结果:使用文字常量长度而不是虚拟常量长度进行编译。
让我先为你澄清一些事情。
在C
中,一个const
变量被认为是const
限定的,它不是编译时常量值(不像整数文字,这是一个编译时间常量值)。因此,根据正常数组大小规范的规则,在这种情况下您甚至不能使用 const
变量。
在C
中,我们可能有使用VLA的规定,这使我们能够使用像[=15=这样的语法] 即使 max
不是 const
变量,但这又是编译器的一些可选功能(根据 C11
)。
在C++
中,我们可以使用一个const
变量作为数组的大小,如在C++
中,一个const
变量是一个编译时间常数.
所以,回答你的问题:
- 在
C
中,如果你的编译器支持VLA,你的代码就没问题。即使 max
不是 const
.
- 在
C++
中没有 VLA,但它可能 作为 gnu 扩展支持。如果max
是const
就可以了
在 C 或 C++ 中,尝试在 stdlib.h
中使用 malloc()
,在 C++ 中使用 cstdlib
。不要忘记 free()
const int max = 1000;
struct pair *ptr = malloc(sizeof(pair) * max); // allocated 1000 pairs
free(ptr); // when the amount of memory is not needed anymore
最简单的解决方法是直接使用
const int max = 1000;
从 class 中取出并放在 class 之上。
更好的方法是确保它是一个编译时常量,如下所示:
constexpr int max = 1000;
此代码无法编译:
#ifndef RemoteControl_h
#define RemoteControl_h
#include "Arduino.h"
class RemoteControl
{
public:
RemoteControl();
~RemoteControl();
static void prev_track();
static void next_track();
static void play_pause_track();
static void mute();
static void vol_up();
static void vol_down();
void respond(int code);
void add_code(int code, void (*func)());
private:
boolean active = true;
struct pair {
int _code;
void (*_func)();
};
const int max = 1000;
int database_length = 0;
pair database[max]; //This line doesn't compile unless I use a literal constant instead of "max"
};
#endif
但是,如果我将下面的部分放在 class 的构造函数中,它就可以正常工作。
const int max = 1000;
int database_length = 0;
pair database[max];
我是否不允许在 c++ 中的 class 中声明一个数组并使用虚拟常量作为长度?如果这有所不同,我正在 arduino 工作,但我希望我不理解 c++ 语言的某些东西,因为这是一个标准的 .h 文件。哦,问题不在于 .cpp 文件,因为我完全删除它并得到相同的结果:使用文字常量长度而不是虚拟常量长度进行编译。
让我先为你澄清一些事情。
在
C
中,一个const
变量被认为是const
限定的,它不是编译时常量值(不像整数文字,这是一个编译时间常量值)。因此,根据正常数组大小规范的规则,在这种情况下您甚至不能使用const
变量。在
C
中,我们可能有使用VLA的规定,这使我们能够使用像[=15=这样的语法] 即使max
不是const
变量,但这又是编译器的一些可选功能(根据C11
)。在
C++
中,我们可以使用一个const
变量作为数组的大小,如在C++
中,一个const
变量是一个编译时间常数.
所以,回答你的问题:
- 在
C
中,如果你的编译器支持VLA,你的代码就没问题。即使max
不是const
. - 在
C++
中没有 VLA,但它可能 作为 gnu 扩展支持。如果max
是const
就可以了
在 C 或 C++ 中,尝试在 stdlib.h
中使用 malloc()
,在 C++ 中使用 cstdlib
。不要忘记 free()
const int max = 1000;
struct pair *ptr = malloc(sizeof(pair) * max); // allocated 1000 pairs
free(ptr); // when the amount of memory is not needed anymore
最简单的解决方法是直接使用
const int max = 1000;
从 class 中取出并放在 class 之上。
更好的方法是确保它是一个编译时常量,如下所示:
constexpr int max = 1000;