Search code examples
compilationethereumsolidity

Solidity Error: Struct containing a (nested) mapping cannot be constructed


My version of solc:

    "solc": "^0.7.1",

When I try to construct a struct that contains mapping, I got this error: "Struct containing a (nested) mapping cannot be constructed"

This is my solidity code.

Campaign.sol

contract Campaign {
struct Request {
    ...
// I guess this might cause an error
    mapping(address => bool) approvals;

}

constructor(uint256 minimum, address creator) {
    ...
}

function createRequest(
    string memory description,
    uint256 value,
    address payable recipient
) public onlyManager {
    Request memory newRequest = Request({ 
        // Here the compiler complains 
    });
}
...

If I want to put a mapping inside of my Request struct, is there any other way around? Thank you


Solution

  • I think I find an answer here

    I put the mapping outside of the struct, and the error is gone:

    contract Campaign {
        struct Request {
            //...
        }
        mapping(address => bool) approvals;
    

    I'm not sure it solves the problem totally. I'll update if there is another issue.