[英]Solidity, Solc Error: Struct containing a (nested) mapping cannot be constructed
I am using Solc version 0.7.0 installed by npm.我正在使用 npm 安装的 Solc 版本 0.7.0。 When I try to create a Struct that contains mapping, I received an error: "Struct containing a (nested) mapping cannot be constructed."当我尝试创建一个包含映射的结构时,我收到一个错误:“无法构造包含(嵌套)映射的结构。”
Please check the code:请检查代码:
// 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);
}
}
When I use older versions of solc, the code compiles without problems.当我使用旧版本的 solc 时,代码编译没有问题。
Thank you in advance!先感谢您!
Take a look at " Solidity v0.7.0 Breaking Changes " chapter in the solidity documentation.查看solidity 文档中的“ Solidity v0.7.0 Breaking Changes ”一章。 Your issue is explained here .你的问题在这里解释。
This worked in my case:这在我的情况下有效:
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;
}
This should work:这应该有效:
function createRequest(uint256 value) public {
Request storage newRequest = requests.push();
newRequest.value = value;
}
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.