将更新和删除功能添加到固体结构

发布于 2025-01-21 15:04:14 字数 909 浏览 0 评论 0原文

我正在尝试将更新功能和删除功能添加到我的结构中,但是它不起作用。

有人知道如何使这些功能起作用吗?

pragma solidity 0.7.5;

contract dogs{

    struct Person{
        uint age;
        string name;
    }
    Person[]people;
    function addNewPerson(uint _age, string memory _name)public {
        Person memory newPerson = Person(_age, _name);
        people.push(newPerson);

    }
    function getPerson(uint _index)public view returns(uint, string memory){
        Person memory personToReturn = people[_index];
        return(personToReturn.age, personToReturn.name);
    }
// to replace and old person with a knew person

    function update(uint _index) public view returns(uint, string memory){
        Person memory updatePerson = people[_index];
    return(updatePerson.age, updatePerson.name);
    }
// delete button for indexs

    function destory(uint _index)public{
        delete people[_index];

    }
}

I'm trying to add an update function and a delete function to my struct, however it doesn't work.

Does someone know how to make those functions work?

pragma solidity 0.7.5;

contract dogs{

    struct Person{
        uint age;
        string name;
    }
    Person[]people;
    function addNewPerson(uint _age, string memory _name)public {
        Person memory newPerson = Person(_age, _name);
        people.push(newPerson);

    }
    function getPerson(uint _index)public view returns(uint, string memory){
        Person memory personToReturn = people[_index];
        return(personToReturn.age, personToReturn.name);
    }
// to replace and old person with a knew person

    function update(uint _index) public view returns(uint, string memory){
        Person memory updatePerson = people[_index];
    return(updatePerson.age, updatePerson.name);
    }
// delete button for indexs

    function destory(uint _index)public{
        delete people[_index];

    }
}

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

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

发布评论

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

评论(1

枕花眠 2025-01-28 15:04:14

尝试下面的智能合约,我还插入了一些评论,以了解您的合同中的错误:

// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

contract Dogs {

    struct Person{
        uint age;
        string name;
    }

    Person[] people;

    function addNewPerson(uint _age, string memory _name)public {
        Person memory newPerson = Person(_age, _name);
        people.push(newPerson);
    }

    function getPerson(uint _index)public view returns(uint, string memory){
        Person memory personToReturn = people[_index];
        return(personToReturn.age, personToReturn.name);
    }

    // to replace and old person with a knew person
    function update(uint _index, uint _newAge, string memory _newName) public returns(uint, string memory) {
        Person memory updatePerson = people[_index];
        updatePerson.age = _newAge;
        updatePerson.name = _newName;
        // You can replace the old person at specific index to a new person.
        // You can do it, using the statement declared below this line  
        people[_index] = updatePerson;
        return(updatePerson.age, updatePerson.name);
    }
    
    // delete button for indexs
    function destory(uint _index) public {
        delete people[_index];
    }
}

如果您执行 delete> delete 函数,请从煤气中收到错误,请记住增加气体限制。
关注addNewperson功能,它起作用。

Try smart contract below, I also inserted some comments to understand you what you wrong in your contract:

// SPDX-License-Identifier: MIT
pragma solidity 0.7.5;

contract Dogs {

    struct Person{
        uint age;
        string name;
    }

    Person[] people;

    function addNewPerson(uint _age, string memory _name)public {
        Person memory newPerson = Person(_age, _name);
        people.push(newPerson);
    }

    function getPerson(uint _index)public view returns(uint, string memory){
        Person memory personToReturn = people[_index];
        return(personToReturn.age, personToReturn.name);
    }

    // to replace and old person with a knew person
    function update(uint _index, uint _newAge, string memory _newName) public returns(uint, string memory) {
        Person memory updatePerson = people[_index];
        updatePerson.age = _newAge;
        updatePerson.name = _newName;
        // You can replace the old person at specific index to a new person.
        // You can do it, using the statement declared below this line  
        people[_index] = updatePerson;
        return(updatePerson.age, updatePerson.name);
    }
    
    // delete button for indexs
    function destory(uint _index) public {
        delete people[_index];
    }
}

If you receive out of gas error when you're executing delete function, remember to increase gas limit.
Concern addNewPerson function, it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文