在Solidity 0.5.0中返回字符串,函数中的返回参数数据位置必须为"memory"。

6
如何在 Solidity 编译器版本 0.5.0 中返回字符串?
contract Test {
    string public text = 'show me';
    function  test() public view returns (string) {
        return text;
    }
}

我收到了错误信息:
TypeError: Data location must be "memory" for return parameter in function, but none was given.
2个回答

11

1
//The version I have used is 0.5.2

pragma solidity ^0.5.2;

contract Inbox{


string public message;

//**Constructor** must be defined using “constructor” keyword

//**In version 0.5.0 or above** it is **mandatory to use “memory” keyword** so as to 
//**explicitly mention the data location**

//you are free to remove the keyword and try for yourself

 constructor (string memory initialMessage) public{
 message=initialMessage;
 }

 function setMessage(string memory newMessage)public{
 message=newMessage;

 }

 function getMessage()public view returns(string memory){
 return message;
 }}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接