如何在结构中初始化数组

How do I initialize an array in a struct

我有一个结构 Purchase,我在其中放置了一系列付款。但是,当我尝试在我的 makePayment 方法中添加新的付款数组时,我从 solidity 编译器返回一个错误:"Internal compiler error: Copying of type struct Payment memory[] memory to storage not yet supported." 当我将 mayment 数组更改为 storagememory,我得到了同样的错误。我在下面添加了相关代码。

有没有可能做我想做的事?我在文档中没有看到任何明确说明这是不可能的,但我也没有看到任何示例在做我想做的事情。 :|

  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);
  }

设置时需要手动更改payments数组的长度。

要么使用:

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

或者:

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

要在 Purchase 中设置 payments 数组,而不是创建数组并尝试将其设置为 Purchase.payments,您可以执行以下操作:

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));

延长购买期限将自动创建新属性。然后你可以手动设置它们。

我发现这是最佳解决方案。

  event OnCreateRoom(address indexed _from, uint256 _value);

   struct Room {
      address[] players;       
      uint256 whosTurnId;
      uint256 roomState;
   }  

   Room[] rooms;

   function createRoom() public{
       address[] adr;
       adr.push(msg.sender);
       Room memory room = Room(adr, 0, 0);   
       rooms.push(room);
       OnCreateRoom(msg.sender, 0);
   }