How to initialize an array in a structure

I have a struct purchase in which I put an array of payments. However, when I try to add a new payment array to my makePayment method, I get an error message from the integrity compiler: "Internal compiler error: copying type struct Memory of payment memory [] to memory is not yet supported." When I modify the flicker array as storage or memory , I get the same error. I have added the appropriate code below.

Is it possible to do what I'm trying to do in strength? I donโ€™t see anything explicitly saying that this is not possible in the documentation, but I also donโ€™t see examples that do what I am 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); } 
+6
source share
1 answer

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

Or use:

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

Or:

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

To configure the payment array in the Purchase instead of creating an array and trying to install it in 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)); 

Expanding the length of purchases will automatically create new attributes. Then you can install them manually.

+7
source

All Articles