如何与映射的映射相互作用以实现结构?

发布于 2025-01-22 08:57:18 字数 1222 浏览 0 评论 0原文

我已经用嵌套映射创建了结构。考虑合同中写入的以下代码:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

contract TestContract {
  struct Item {
    uint itemId;
    uint itemValue;
  }

  struct OverlayingItemStruct {
    mapping(uint => Item) items;
    uint overlayingId;
    uint itemsCount;
  }
  mapping(uint => OverlayingItemStruct) public overlayingItems;
  uint public overlayCount;

  function addOverlayingItemsStruct(uint _value) external {
    overlayCount ++;
    mapping(uint => Item) memory items;
    items[1] = Item(1, _value);
    overlayingItems[overlayCount] = OverlayingItemStruct(items, 1, 1);
  }

  function addItem(uint _value, uint _overlayId) external {
    OverlayingItemStruct storage overlay = overlayingItems[_overlayId];
    overlay.itemsCount ++;

    overlay.items[overlay.itemsCount] = Item(overlay.itemsCount, _value);
  }
}

编译上述代码时,我会遇到错误:

TypeError: Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable.
  --> TestC.sol:21:5:
   |
21 |     mapping(uint => Item) memory items;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

不确定,如何处理此错误。 其次,需要确认在嵌套映射字典中添加新值的方法是否正确?

I have created struct with nested mapping. Consider the following piece of code written inside the contract in solidity:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

contract TestContract {
  struct Item {
    uint itemId;
    uint itemValue;
  }

  struct OverlayingItemStruct {
    mapping(uint => Item) items;
    uint overlayingId;
    uint itemsCount;
  }
  mapping(uint => OverlayingItemStruct) public overlayingItems;
  uint public overlayCount;

  function addOverlayingItemsStruct(uint _value) external {
    overlayCount ++;
    mapping(uint => Item) memory items;
    items[1] = Item(1, _value);
    overlayingItems[overlayCount] = OverlayingItemStruct(items, 1, 1);
  }

  function addItem(uint _value, uint _overlayId) external {
    OverlayingItemStruct storage overlay = overlayingItems[_overlayId];
    overlay.itemsCount ++;

    overlay.items[overlay.itemsCount] = Item(overlay.itemsCount, _value);
  }
}

When compiling the above code, I am getting the error:

TypeError: Uninitialized mapping. Mappings cannot be created dynamically, you have to assign them from a state variable.
  --> TestC.sol:21:5:
   |
21 |     mapping(uint => Item) memory items;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Not sure, how to handle this error.
Secondly, needed to confirm if the method to add new values in the nested mapping dictionary is correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏大泽ㄣ 2025-01-29 08:57:18

在函数和内存状态中,不能在函数中声明映射。此类型仅具有存储状态。
这样说,要将映射传递到一个结构中,您可以将嵌套映射的钥匙设置为内部的相对结构。
我试图这样调整您的智能合约:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract TestContract {

  struct Item {
    uint itemId;
    uint itemValue;
  }

  struct OverlayingItemStruct {
    mapping(uint => Item) items;
    uint overlayingId;
    uint itemsCount;
  }

  mapping(uint => OverlayingItemStruct) public overlayingItems;
  
  uint public overlayCount;

  function addOverlayingItemsStruct(uint _value) external {
    overlayCount ++;
    // NOTE: I declared a new Item struct
    Item memory item = Item(1, _value);
    // NOTE: I set into items mapping key value 1, Item struct created in row above this 
    overlayingItems[overlayCount].items[1] = item;
  }

  function addItem(uint _value, uint _overlayId) external {
    OverlayingItemStruct storage overlay = overlayingItems[_overlayId];
    overlay.itemsCount ++;
    overlay.items[overlay.itemsCount] = Item(overlay.itemsCount, _value);
  }
}  

A mapping cannot be declare inside a function and in memory state. This type has only storage state.
Said this, to pass a mapping into a struct, you can set the key of nested mapping and pass relative struct inside it.
I tried to adjust your smart contract like this:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract TestContract {

  struct Item {
    uint itemId;
    uint itemValue;
  }

  struct OverlayingItemStruct {
    mapping(uint => Item) items;
    uint overlayingId;
    uint itemsCount;
  }

  mapping(uint => OverlayingItemStruct) public overlayingItems;
  
  uint public overlayCount;

  function addOverlayingItemsStruct(uint _value) external {
    overlayCount ++;
    // NOTE: I declared a new Item struct
    Item memory item = Item(1, _value);
    // NOTE: I set into items mapping key value 1, Item struct created in row above this 
    overlayingItems[overlayCount].items[1] = item;
  }

  function addItem(uint _value, uint _overlayId) external {
    OverlayingItemStruct storage overlay = overlayingItems[_overlayId];
    overlay.itemsCount ++;
    overlay.items[overlay.itemsCount] = Item(overlay.itemsCount, _value);
  }
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文