Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize an array in a struct

I have a struct Purchase in which I'm putting an array of payments. However, when I try to add the new payments array in my makePayment method I get an error back from the solidity compiler: "Internal compiler error: Copying of type struct Payment memory[] memory to storage not yet supported." When I change the mayment array to be storage or memory, I get the same error. I've added the relevant code below.

Is it possible to do what I'm trying to do in solidity? I don't see anything explicitly saying it's not possible in the documentation but I also don't see any examples doing what I'm trying to do. :|

  struct Payment {
    address maker;
    uint amount;
  }

  struct Purchase {
    uint product_id;
    bool complete;
    Payment[] payments;
  }
  Purchase[] purchases;

  function makePayment(uint product_id, uint amt, uint purchase_id) returns (bool) {

      Payment[] payments;
      payments[0] = Payment(address, amt);
      purchases[purchase_id] = Purchase(product_id, false, payments);
  }
like image 996
unflores Avatar asked Mar 02 '16 10:03

unflores


1 Answers

You need to manually change the length of the payments array when setting it.

Either use:

  Payment[] payments;
  payments[payments.length++] = Payment(address, amt);

Or:

Payment[] payments;
payments.push(Payment(address, amt));

For setting the payments array in Purchase, instead of creating an array and trying to set it to the Purchase.payments you can do the following:

uint purchase_id = purchases.length++;
purchases[purchase_id].product_id = product_id;
purchases[purchase_id].complete   = false;
purchases[purchase_id].payments.push(Payment(msg.sender, amt));

Extending the purchases length will automatically create the new attributes. Then you can set them manually.

like image 197
unflores Avatar answered Oct 10 '22 07:10

unflores