【问题标题】:Solidity, Solc Error: Struct containing a (nested) mapping cannot be constructedSolidity,Solc 错误:无法构造包含(嵌套)映射的结构
【发布时间】:2020-11-20 00:47:54
【问题描述】:

我正在使用 npm 安装的 Solc 版本 0.7.0。当我尝试创建包含映射的 Struct 时,收到错误消息:“无法构造包含(嵌套)映射的结构。”

请检查代码:

// SPDX-License-Identifier: MIT
pragma solidity 0.7.0;

contract Test {
    struct Request {
        uint256 value;
        mapping(address => bool) approvals;
    }
    Request[] public requests;
      ...

    function createRequest(
        uint256 value
    ) public {
        Request memory newRequest = Request({// here the compiler complains
            value: value
        });

        requests.push(newRequest);
    }
}

当我使用旧版本的 solc 时,代码编译没有问题。

提前谢谢你!

【问题讨论】:

标签: struct mapping blockchain ethereum solidity


【解决方案1】:

这应该可行:

function createRequest(uint256 value) public {
    Request storage newRequest = requests.push();
    newRequest.value = value;
}

干杯!

【讨论】:

    【解决方案2】:

    这在我的情况下有效:

    struct Request{
        uint256 value;
        mapping(address => bool) approvals;
    }
                
    uint256 numRequests;
    mapping (uint256 => Request) requests;
            
    function createRequest (uint256 value) public{
        Request storage r = requests[numRequests++];
        r.value= value;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-29
      • 1970-01-01
      • 2021-10-13
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      相关资源
      最近更新 更多