如何在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.
答案 0 :(得分:3)
只需在memory
之后添加string
,就像这样:
function test() public view returns (string memory) {
另一个变化: https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html#interoperability
答案 1 :(得分: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;
}}