【问题标题】:Return string in solidity 0.5.0. Data location must be "memory" for return parameter in function以solidity 0.5.0返回字符串。函数中返回参数的数据位置必须是“内存”
【发布时间】:2019-04-26 02:51:36
【问题描述】:

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.

【问题讨论】:

    标签: ethereum solidity smartcontracts


    【解决方案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;
     }}
    

    【讨论】:

      【解决方案2】:

      只需要在string之后加上memory,像这样:

      function test() public view returns (string memory) {
      

      另一个变化: https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html#interoperability

      【讨论】:

        猜你喜欢
        • 2020-02-04
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 2019-12-15
        • 2021-03-15
        • 1970-01-01
        • 2019-04-29
        相关资源
        最近更新 更多