将更新和删除功能添加到固体结构
我正在尝试将更新功能和删除功能添加到我的结构中,但是它不起作用。
有人知道如何使这些功能起作用吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试下面的智能合约,我还插入了一些评论,以了解您的合同中的错误:
如果您执行 delete> delete 函数,请从煤气中收到
错误,请记住增加气体限制。
关注
addNewperson
功能,它起作用。Try smart contract below, I also inserted some comments to understand you what you wrong in your contract:
If you receive
out of gas
error when you're executingdelete
function, remember to increase gas limit.Concern
addNewPerson
function, it works.