0

First of all, sorry for my English as a Google Translate

I wanted to add values to the list inside the struct, but with the code below the following error appears: UnimplementedFeatureError: Copying of type struct Ballot.Play memory [] memory to storage not yet supported.

pragma solidity ^0.6.7;
contract Ballot {

    struct Play {
        uint cod_music;
        uint qtd;
    }

    struct Listener{
        address user;
        Play[] listened;
    }

    Listener[] listeners;
    Play[] tests;

    function listenMusic(uint cod) public {
        tests.push(Play({
            cod_music: cod,
            qtd: 1
        }));

        listeners.push(Listener({
            user: msg.sender,
            listened: tests
        }));
    }
}

Any suggestion?

1

Yes, change this:

listeners.push(Listener({
    user: msg.sender,
    listened: tests
}));

To this:

listeners.length += 1;
Listener storage listener = listeners[listeners.length - 1];
listener.user = msg.sender;
for (uint i = 0; i < tests.length; i++)
    listener.listened.push(tests[i]);

Or even to this:

listeners.length += 1;
Listener storage listener = listeners[listeners.length - 1];
listener.user = msg.sender;
listener.listened = tests;

Side-note: I haven't tested either one of them.

3
  • browser/StackExchange.sol:23:9: TypeError: Member "length" is read-only and cannot be used to resize arrays. listeners.length += 1; ^--------------^ May 5 '20 at 20:18
  • @JoãoVitor: It works on solc 0.4.25, and at least according to this answer, it should be allowed. Not sure about solc 0.6.x. Sorry :( May 5 '20 at 20:19
  • Thanks, it worked May 5 '20 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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