6

I am working on creating a basic voting smart contract. But when assigning Question obj in the map, I'm getting this error:

pragma solidity ^0.8.0;

contract Voting {
    struct Question {
        uint256 questionId;
        Option[] options;
    }

    struct Option {
        uint256 optionId;
        string optionValue;
    }

    // map(questionId => Question)
    mapping(uint256 => Question) public mapQuestions;

    function addQuestion(string memory _option1) public {
        Question memory quest;
        quest.questionId = 1;
        quest.options[0] = Option(1, _option1);

        mapQuestions[quest.questionId] = quest;
    }
}

When compiling, I'm getting this error:

copying of type struct Voting.Option memory[] memory to storage not yet supported

1 Answer 1

8

Arrays and mappings are only allocated to storage right now, and when you create Question in memory you try to allocate the array inside to memory. You would usually get a reference onto the struct from a mapping and work with it like that:

function addQuestion(string calldata _option1) public {
    uint _id = 1;
    Question storage quest = mapQuestions[_id];
    quest.questionId = _id;
    quest.options.push(Option(_id, _option1));
}
1
  • 1
    This worked like a charm! Thanks @Kostiantyn for providing a simplistic yet elegant solution.
    – VikasGS
    Dec 30, 2021 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.