13

我有一个结构购买,我在其中放置了一系列付款。但是,当我尝试在我的makePayment方法中添加新的支付数组时,我从solidity 编译器返回一个错误:“内部编译器错误:将类型struct Payment memory[] memory 复制到尚不支持的存储。” 当我将 mayment 数组更改为storageormemory时,我得到了同样的错误。我在下面添加了相关代码。

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

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

2 回答 2

9

您需要在设置时手动更改支付数组的长度。

要么使用:

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

或者:

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

要在 Purchase 中设置支付数组,而不是创建一个数组并尝试将其设置为 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));

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

于 2016-03-02T13:25:51.897 回答
1

我发现这是最好的解决方案。

  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);
   }
于 2021-10-14T01:48:20.913 回答