成员“推”在地址[3]内存中与参数有关查找后找不到或看不到

发布于 2025-02-06 19:40:45 字数 724 浏览 0 评论 0原文

我得到错误

Member "push" not found or not visible after argument-dependent lookup in address[3] memory.

函数,在此错误中,

function initiateTransaction( address payable _to, uint _amount ) public onlyUser returns(uint txnId) {
        transaction memory newTxn;
        newTxn.id = ++txnCount;
        newTxn.amount = _amount;
        newTxn.to = _to;
        newTxn.initiatedBy = msg.sender;
        newTxn.signedBy.push(msg.sender); // I'm getting error here.
        
    }

我已经定义了结构如下

    struct transaction {
        uint id;
        uint amount;
        address payable to;
        address initiatedBy;
        address[3] signedBy;
    }

I'm getting error

Member "push" not found or not visible after argument-dependent lookup in address[3] memory.

function in which I'm getting this error is

function initiateTransaction( address payable _to, uint _amount ) public onlyUser returns(uint txnId) {
        transaction memory newTxn;
        newTxn.id = ++txnCount;
        newTxn.amount = _amount;
        newTxn.to = _to;
        newTxn.initiatedBy = msg.sender;
        newTxn.signedBy.push(msg.sender); // I'm getting error here.
        
    }

I've defined the struct as follows

    struct transaction {
        uint id;
        uint amount;
        address payable to;
        address initiatedBy;
        address[3] signedBy;
    }

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

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

发布评论

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

评论(1

谁的年少不轻狂 2025-02-13 19:40:45

签名是一个固定大小的数组,因此您无法使用push()来调整它大小。

您需要跟踪数组中有多少个非零值并重写正确的项目。 (或动态计算索引,但在气体成本方面的优化程度较低。)

struct transaction {
    uint id;
    uint amount;
    address payable to;
    address initiatedBy;
    uint signersCount; // default value 0
    address[3] signedBy;
}

function initiateTransaction( address payable _to, uint _amount ) public onlyUser returns(uint txnId) {
    transaction memory newTxn;
    newTxn.id = ++txnCount;
    newTxn.amount = _amount;
    newTxn.to = _to;
    newTxn.initiatedBy = msg.sender;
    // retrieve `signersCount`, use it as the `signedBy` index, and then increment it
    newTxn.signedBy[newTxn.signersCount++] = msg.sender;
}

signedBy is a fixed-size array, so you cannot resize it by using push().

You need to keep track of how many non-zero values are in the array and rewrite the correct item. (Or calculate the index dynamically, but that would be less optimized in terms of gas costs.)

struct transaction {
    uint id;
    uint amount;
    address payable to;
    address initiatedBy;
    uint signersCount; // default value 0
    address[3] signedBy;
}

function initiateTransaction( address payable _to, uint _amount ) public onlyUser returns(uint txnId) {
    transaction memory newTxn;
    newTxn.id = ++txnCount;
    newTxn.amount = _amount;
    newTxn.to = _to;
    newTxn.initiatedBy = msg.sender;
    // retrieve `signersCount`, use it as the `signedBy` index, and then increment it
    newTxn.signedBy[newTxn.signersCount++] = msg.sender;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文