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

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

struct Campaign {
        address payable campaignOwner; 
        string campaignTitle;
        string campaignDescription; 
        uint256 goalAmount;
        uint256 totalAmountFunded;
        uint256 deadline;
        bool goalAchieved; 
        bool isCampaignOpen; 
        bool isExists;

        mapping(address => uint256) contributions;
    }
   
    //stores a Campaign struct for each unique campaign ID.
    mapping(uint256 => Campaign) campaigns; 

function createCampaign(string memory _campaignTitle, string memory _campaignDescription, uint256 _goalAmount, uint256 _fundingPeriodInDays ) public {
        ++totalCampaigns;

        uint256 period = block.timestamp + (_fundingPeriodInDays * 1 days);

        Campaign memory aCampaign = Campaign(payable(msg.sender),_campaignTitle, _campaignDescription, _goalAmount, 0, period , false, true, true);
        campaigns[totalCampaigns] = aCampaign; 
     } 

我使用的是 Solc 0.8.0 版。当我尝试创建一个包含映射的结构时,我收到一个错误:

Struct containing a (nested) mapping cannot be constructed.

当我使用旧版本的 solc (0.5.8) 时,代码编译没有问题。但是这个版本不支持其他 solidity 文件并给出错误:

Source file requires different compiler version (current compiler is 0.8.13+commit.abaa5c0e.Emscripten.clang) - note that nightly builds are considered to be strictly less than the released version

不能在内存中保留包含映射的结构,也不能在函数中初始化存储对象,它必须是状态变量。所以你可以做的是,从你的映射中获取一个对象,将它分配给一个局部变量并修改它。像这样:

function createCampaign(string memory _campaignTitle, string memory _campaignDescription, uint256 _goalAmount, uint256 _fundingPeriodInDays ) public {
        ++totalCampaigns;

        uint256 period = block.timestamp + (_fundingPeriodInDays * 1 days);

        Campaign storage aCampaign = campaigns[totalCampaigns];

        aCampaign.campaignOwner = payable(msg.sender);
        aCampaign.campaignTitle = _campaignTitle;
        aCampaign.campaignDescription = _campaignDescription;
        aCampaign.goalAmount = _goalAmount;
        aCampaign.totalAmountFunded = 0;
        aCampaign.deadline = period;
        aCampaign.goalAchieved = false;
        aCampaign.isCampaignOpen = true;
        aCampaign.isExists = true;
     }