Solidity
Solidity 錯誤:無法構造包含(嵌套)映射的結構
我的solc版本:
"solc": "^0.7.1",
當我嘗試構造一個包含映射的結構時,出現此錯誤:“無法構造包含(嵌套)映射的結構”
這是我的可靠程式碼。
活動.sol
pragma solidity >=0.5.0; contract Campaign { struct Request { string description; // I declared a mapping inside of a struct. mapping(address => bool) approvals; } constructor(uint256 minimum, address creator) { ... } function createRequest(string memory description) public onlyManager { Request memory newRequest = Request({ // Here the compiler gives me an error description : description }); } ...
它說
Struct containing a (nested) mapping cannot be constructed.
請幫我。
問題出在構造中,即,
Request({ description: description })
因此更改newRequest
frommemory
到的位置storage
將無濟於事。如果您真的想讓它工作,請創建一個狀態變數,例如
mapping (uint => Request) requests
. 現在,在您的函式中,您可以編寫稍後將遞增的Request storage newRequest = requests[index]
位置。index
有關參考,請參閱:文件
從 0.7.0 開始,如下所示:
struct Request{ string description; uint value; address recipient; bool complete; uint approvalsCount; mapping(address => bool) approvals; } uint numRequests; mapping (uint => Request) requests; function createRequest (string memory description, uint value, address recipient) public{ Request storage r = requests[numRequests++]; r.description = description; r.value = value; r.recipient = recipient; r.complete = false; r.approvalsCount = 0; }