2

I'm aware there are some limitations on initializing empty arrays, but I can't find a solution to this issue I'm having.

I want to push a struct to an array. But since that struct itself contains an array of other structs, I'm unable to do it.

Here's my code:

pragma solidity ^0.4.23;
pragma experimental ABIEncoderV2;



contract simpleBet {

    struct Bettor {
        address wallet;
        uint betAmount;
        uint outcome;
        uint bettingTime;
    }

    struct bettingEvent {
        string name;
        string uid;
        uint startTime;
        uint endTime;
        Bettor[] bettors;
    }

    bettingEvent[] public bettingEvents;

    constructor() public {
    }


    function createBettingEvent(string _name, string _uid, uint _startTime, uint _endTime) public {

        bettingEvent memory _bettingEvent;
        _bettingEvent.name = _name;
        _bettingEvent.uid = _uid;
        _bettingEvent.startTime = _startTime;
        _bettingEvent.endTime = _endTime;
        _bettingEvent.bettors.push(Bettor(0,0,0,0));


        bettingEvents.push(_bettingEvent);
    }

}

TypeError: Member "push" is not available in struct simpleBet.Bettor memory[] memory outside of storage.

1 Answer 1

2

The issue is that you can't push to arrays that are located in memory. (Such arrays are not resizable.)

One possible fix is to just do the changes in storage:

function createBettingEvent(string _name, string _uid, uint _startTime, uint _endTime) public {
    bettingEvents.length += 1;
    bettingEvent storage _bettingEvent = bettingEvents[bettingEvents.length - 1];
    _bettingEvent.name = _name;
    _bettingEvent.uid = _uid;
    _bettingEvent.startTime = _startTime;
    _bettingEvent.endTime = _endTime;
    _bettingEvent.bettors.push(Bettor(0, 0, 0, 0));
}
8
  • Thanks. I've tried that, but still get the error: UnimplementedFeatureError: Copying of type struct simpleBet.Bettor memory[] memory to storage not yet supported.
    – Cornwell
    Commented May 5, 2018 at 18:40
  • I don't get that error on Remix. Can you update the question with the full latest source code so I can try to reproduce the error?
    – user19510
    Commented May 5, 2018 at 18:41
  • Oh, sorry, I do see that error in the compiler output; Remix just didn't surface it in the editor UI for some reason. Taking a look.
    – user19510
    Commented May 5, 2018 at 18:42
  • 1
    Glad you're enjoying Capture the Ether. :-) Yeah, I wonder if a mapping might be better. Mappings and arrays are remarkably similar. See the "Queue" section of programtheblockchain.com/posts/2018/03/23/… for a spot where I used a mapping instead of an array.
    – user19510
    Commented May 5, 2018 at 18:54
  • 1
    It's declaring a local variable named _bettingEvent, which is a reference to a bettingEvent struct located in storage. It's then assigning that variable a reference to the last element of the bettingEvents array.
    – user19510
    Commented Mar 23, 2019 at 18:18

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.