gpt4 book ai didi

arrays - Arduino 类数组成员

转载 作者:行者123 更新时间:2023-12-01 22:24:28 29 4
gpt4 key购买 nike

我正在做一个 Arduino 项目,我有一个有数组成员的类,这很好,但我想做的是能够在初始化时初始化成员数组。因此,每次使用 Class 时,都可以将数组初始化为可变长度。这是我正在尝试的,但我不断收到编译器错误“数组成员的初始值设定项无效”。我不想将每个数组初始化为最大长度,这会导致分配空间浪费。

标题

class ClassA{

int items[];

ClassA(int Items[]);
};

来源

ClassA::ClassA(int Items[]) : items(Items){

}

然后像这样初始化它:

ClassA obj1({1, 2, 3, 4, 5});
ClassA obj2({6, 7});
ClassA obj3({8, 9, 10, 11, 12, 13, 14, 15});

最佳答案

也许您来自其他高级语言(例如 C# 或 Java),但是...在 C/C++(和 arduino,它们的子语言)中,您有两种声明数组的方法:

  1. 静态分配:int Items[N_ELEM];
  2. 动态分配:int *Items;

没有 int Items[]; 这样的东西,因为你没有在编译时保留空间。

现在,如果你想在运行时修改它,你有两种方法。

  1. 使用静态分配的数组;在这种情况下,您将需要在编译时分配它,因此您必须输入足够大的大小以容纳最大的数组。
  2. 使用动态分配的数组;在这种情况下,还请实现析构函数以释放内存。

例子:方法一:

#define MAX_ITEMS 8 // Enter the largest value for the items length
class ClassA{
int items[MAX_ITEMS];
byte itemsLength;

ClassA(int *Items, byte ItemsLength);
};


ClassA::ClassA(int *Items, byte ItemsLength){
if (ItemsLength > MAX_ITEMS)
itemsLength = MAX_ITEMS;
else
itemsLength = ItemsLength;
byte i;
for (i = 0; i < itemsLength; i++)
items[i] = Items[i];
}


int array1[] = {1, 2, 3, 4, 5};
int array3[] = {8, 9, 10, 11, 12, 13, 14, 15};

// Three ways to initialize it
ClassA obj1(array1, 5);
ClassA obj2({6, 7}, 2);
ClassA obj3(array3, sizeof(array3)/sizeof(int));

方法2a:使用malloc

class ClassA{
int *items;
byte itemsLength;

ClassA(int *Items, byte ItemsLength);
~ClassA();
};


ClassA::ClassA(int *Items, byte ItemsLength){
itemsLength = ItemsLength;
items = malloc(itemsLength * sizeof(int));
byte i;
for (i = 0; i < itemsLength; i++)
items[i] = Items[i];
}

ClassA::~ClassA(){
free items;
}


int array1[] = {1, 2, 3, 4, 5};
int array3[] = {8, 9, 10, 11, 12, 13, 14, 15};

// Three ways to initialize it
ClassA obj1(array1, 5);
ClassA obj2({6, 7}, 2);
ClassA obj3(array3, sizeof(array3)/sizeof(int));

方法 2b:仅当数组未被删除时可用(即您将它们声明为全局静态变量,而不是在函数中)

class ClassA{
int *items;
byte itemsLength;

ClassA(int *Items, byte ItemsLength);
};


ClassA::ClassA(int *Items, byte ItemsLength){
// You just store the pointer, not the values
// no need for a free
itemsLength = ItemsLength;
items = Items;
}


int array1[] = {1, 2, 3, 4, 5};
int array3[] = {8, 9, 10, 11, 12, 13, 14, 15};

// Only two ways to initialize it
ClassA obj1(array1, 5);
ClassA obj2({6, 7}, 2); /* YOU CAN'T USE THIS */
ClassA obj3(array3, sizeof(array3)/sizeof(int));

方法 2c:使用 C++ 数组

class ClassA{
int *items;
byte itemsLength;

ClassA(int *Items, byte ItemsLength);
~ClassA();
};


ClassA::ClassA(int *Items, byte ItemsLength){
itemsLength = ItemsLength;
items = new int[itemsLength];
byte i;
for (i = 0; i < itemsLength; i++)
items[i] = Items[i];
}

ClassA::~ClassA(){
delete[] items;
}


int array1[] = {1, 2, 3, 4, 5};
int array3[] = {8, 9, 10, 11, 12, 13, 14, 15};

// Three ways to initialize it
ClassA obj1(array1, 5);
ClassA obj2({6, 7}, 2);
ClassA obj3(array3, sizeof(array3)/sizeof(int));

也许在最后一种情况下你可以将构造函数推导到

ClassA::ClassA(int Items[]):items(Items)
{
}

但你必须测试它,因为我不知道它是否会起作用

最好的问候

关于arrays - Arduino 类数组成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074763/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com