无法在solidity中创建输入大小的数组

问题描述 投票:0回答:1

我正在尝试使用函数输入一个数字并创建一个给定大小的数组 - 下面的代码和错误。

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];
                              ^---^
arrays ethereum solidity
1个回答
0
投票

在 Solidity 中,您无法创建具有预定义大小的数组,将所有元素初始化为特定值(例如零)。 Solidity 不支持像 Python 的 NumPy 那样创建具有预填充元素默认值的数组。另一种方法是最初将其声明为动态,并且在函数中您可以创建另一个固定数组,并使用循环来设置其值并将其添加到全局动态数组中。

© www.soinside.com 2019 - 2024. All rights reserved.