UnimplementedFeatureError: Copying of type struct Election.Candidate memory[] memory to storage not yet supported.

The Error only appeared when I added the addBallot function.

struct Candidate {
    string name;
    string department;
    string faculty;
    uint voteCount;
    string manifesto;
    string image;
}

Voter[] public voters;
Candidate[] public candidates;
Ballot[] public ballots;

function addBallot(string memory ballot_name,
    uint[] memory candidate_indexes,
    string memory key,
    string memory value
   ) public {
    Candidate[] memory cands;
    Voter[] memory voters_list;
    
    for (uint i = 0; i < candidate_indexes.length; i++) {
        cands[cands.length] = candidates[i];
    }
    if (keccak256(abi.encodePacked(key)) == keccak256(abi.encodePacked("all"))) {
        voters_list = voters;
    }
    else if (keccak256(abi.encodePacked(key)) == keccak256(abi.encodePacked("fac"))) {
        for (uint i = 0; i < voters.length; i++) {
            if (keccak256(abi.encodePacked(voters[i].faculty)) == keccak256(abi.encodePacked(value))) {
                voters_list[voters_list.length] = voters[i];
            }
        }
    }
    else {
        for (uint i = 0; i < voters.length; i++) {
            if (keccak256(abi.encodePacked(voters[i].department)) == keccak256(abi.encodePacked(value))) {
                voters_list[voters_list.length] = voters[i];
            }
        }
    }

    ballots.push(Ballot({name: ballot_name, candidates: cands, allowedVoters: voters_list, totalVotes: 0}));
}
1

There are 1 best solutions below

0
On

The issue is in one of your assignments, probably this one:

voters_list = voters;

The Solidity compiler currently does not support copying whole arrays of structs between storage and memory. If you want to do that, you have do it explicitly, by copying these structs one by one.

Unfortunately the error reporting around this particular limitation is pretty bad and the compiler does not tell you where exactly this happens. Instead it crashes in the codegen. See UnimplementedFeatureError with no line indication when copying an array of structs to storage #12783.