20 返回结构数组遇到 Member "push" is not available in struct,该怎么解决呢?

如题,我的需求是想返回个结构数组,从而获得一个city里所有goods的name,price,quantity信息。里面使用了push方法,但遇到这个报错。网上查了下好像是memory的变量不能被push,但换成storage后又会遇到其它报错。请问该怎么办呢? image.png

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

contract Market {
    struct City {
        uint256 cityID;
        string cityName;
        mapping(uint256 => SingleGoods) goods;
    }

    mapping(uint256 => City) citys;

    // 表示某city的某goodsid是否存在。第一个uint是cityid,第二个是goodsid
    mapping(uint256 => mapping(uint256 => bool)) public goodsWhetherInserted;

    // 将某city的所有goodsid保存到一个数组中
    mapping(uint256 => uint256[]) public ids;

    struct SingleGoods {
        string name;
        uint256 price;
        uint256 quantity;
    }

    function createCity(uint256 _cityID, string memory _cityName) public {
        City storage city = citys[_cityID];
        city.cityID = _cityID;
        city.cityName = _cityName;
    }

    function addGoodsToCity(
        uint256 _cityID,
        uint256 _goodsID,
        string memory _goodsName,
        uint256 _goodsPrice,
        uint256 _goodsQuantity
    ) public {
        City storage city = citys[_cityID];
        SingleGoods storage singleGoods = city.goods[_goodsID];
        singleGoods.name = _goodsName;
        singleGoods.price = _goodsPrice;
        singleGoods.quantity = _goodsQuantity;
        goodsWhetherInserted[_cityID][_goodsID] = true;
        ids[_cityID].push(_goodsID);
    }

    function getCityName(uint256 _cityID)
        public
        view
        returns (uint256 cityID, string memory cityName)
    {
        City storage city = citys[_cityID];
        return (city.cityID, city.cityName);
    }

    function getCityGoods(uint256 _cityID)
        public
        view
        returns (SingleGoods[] memory)
    {
        SingleGoods[] memory allGoods;
        SingleGoods memory singleGoods;
        for (uint256 index = 0; index < ids[_cityID].length; index++) {
            singleGoods.name = citys[_cityID].goods[ids[_cityID][index]].name;
            singleGoods.price = citys[_cityID].goods[ids[_cityID][index]].price;
            singleGoods.quantity = citys[_cityID]
                .goods[ids[_cityID][index]]
                .quantity;
            allGoods.push(singleGoods);
        }
        return allGoods;
    }
}
请先 登录 后评论

最佳答案 2022-03-16 10:40

内存数组不能push,可以先定义一个对应长度的数组,然后设置,如下:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.0 <0.9.0;

contract Market {
    struct City {
        uint256 cityID;
        string cityName;
        mapping(uint256 => SingleGoods) goods;
    }

    mapping(uint256 => City) citys;

    // 表示某city的某goodsid是否存在。第一个uint是cityid,第二个是goodsid
    mapping(uint256 => mapping(uint256 => bool)) public goodsWhetherInserted;

    // 将某city的所有goodsid保存到一个数组中
    mapping(uint256 => uint256[]) public ids;

    struct SingleGoods {
        string name;
        uint256 price;
        uint256 quantity;
    }

    function createCity(uint256 _cityID, string memory _cityName) public {
        City storage city = citys[_cityID];
        city.cityID = _cityID;
        city.cityName = _cityName;
    }

    function addGoodsToCity(
        uint256 _cityID,
        uint256 _goodsID,
        string memory _goodsName,
        uint256 _goodsPrice,
        uint256 _goodsQuantity
    ) public {
        City storage city = citys[_cityID];
        SingleGoods storage singleGoods = city.goods[_goodsID];
        singleGoods.name = _goodsName;
        singleGoods.price = _goodsPrice;
        singleGoods.quantity = _goodsQuantity;
        goodsWhetherInserted[_cityID][_goodsID] = true;
        ids[_cityID].push(_goodsID);
    }

    function getCityName(uint256 _cityID)
        public
        view
        returns (uint256 cityID, string memory cityName)
    {
        City storage city = citys[_cityID];
        return (city.cityID, city.cityName);
    }

    function getCityGoods(uint256 _cityID)
        public
        view
        returns (SingleGoods[] memory)
    {
        SingleGoods[] memory allGoods = new SingleGoods[](ids[_cityID].length);
        SingleGoods memory singleGoods;
        for (uint256 index = 0; index < ids[_cityID].length; index++) {
            singleGoods.name = citys[_cityID].goods[ids[_cityID][index]].name;
            singleGoods.price = citys[_cityID].goods[ids[_cityID][index]].price;
            singleGoods.quantity = citys[_cityID]
                .goods[ids[_cityID][index]]
                .quantity;
            allGoods[index] = singleGoods;
        }
        return allGoods;
    }
}
请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 0 收藏,4549 浏览
  • happypl 提出于 2022-03-16 10:22