我正在尝试使用函数输入一个数字并创建一个给定大小的数组 - 下面的代码和错误。
struct Product {
uint PId;
string PName;
string Brand;
uint[] quant_clicks; //This array is supposed to store no. of clicks for each piece of product,
//so its length will be the inputted quantity
SCSection[] SChain;
}
p.quant_clicks = uint[quant]; //When product is being created, array of input size must be created
//to store clicks for each piece (part of a function taking quant & other info as input)
TypeError: Integer constant expected.
p.quant_clicks = uint[quant];
^---^
在 Solidity 中,您无法创建具有预定义大小的数组,将所有元素初始化为特定值(例如零)。 Solidity 不支持像 Python 的 NumPy 那样创建具有预填充元素默认值的数组。另一种方法是最初将其声明为动态,并且在函数中您可以创建另一个固定数组,并使用循环来设置其值并将其添加到全局动态数组中。