问题回顾:在编写完智能合约之后,通过命令行的形式编译智能合约时出现的问题。
1. 具体合约代码(错误写法):
pragma solidity >=0.4.21 <0.7.0;
contract Simple {
string name;
uint age;
//定义事件
event Instructor(string name,uint age);
function set(string _name, uint _age) public {
name=_name;
age=_age;
//触发事件
emit Instructor(name,age);
}
function get() public view returns(string,uint) {
return (name,age);
}
}
2. 具体问题描述(编译有问题的代码已经通过----的形式标注出来了):

3. 问题出现的原因:
这是由于solidity 0.5.0版本的更新导致的,只需要在用到string的时候,在其后面加上memory就可以了。
4. 经过修改后的正确写法如下:
pragma solidity >=0.4.21 <0.7.0;
contract Simple {
string name;
uint age;
//定义事件
event Instructor(string name,uint age);
function set(string memory _name, uint _age) public {
name=_name;
age=_age;
//触发事件
emit Instructor(name,age);
}
function get() public view returns(string memory,uint) {
return (name,age);
}
}
注意:string _name修改为string memory _name
5. 重新编译一下,就显示已经编译成功啦~~

本文解决在Solidity0.5.0版本中编译智能合约遇到的问题,特别是涉及string类型参数时的编译错误。通过在string类型后添加memory关键字,可以成功解决编译问题。

788

被折叠的 条评论
为什么被折叠?



