【bug解决】TypeError: Data location must be "memory" for parameter in function, but none was given.

Julie ·
更新时间:2024-09-20
· 862 次阅读

问题回顾:在编写完智能合约之后,通过命令行的形式编译智能合约时出现的问题。

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. 重新编译一下,就显示已经编译成功啦~~


作者:qqz_2016



None IN for location memory

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章