类型错误:在函数参数中,数据位置必须为“memory”,但未给出任何位置。

16

我尝试编译我的代码,但是遇到了以下错误:

TypeError: 数据位置必须在函数中声明为“memory”,但是没有给出

我的代码:

pragma solidity ^0.5.0;

contract memeRegistry {
    string url;
    string name;
    uint timestamp;

    function setmeme(string _url,string _name, uint _timestamp) public{
        url = _url;
        name = _name;
        timestamp = _timestamp;   
    }
}
5个回答

27

现在,对于所有结构体、数组或映射类型的变量,都必须明确指定数据位置。这也适用于函数参数和返回变量。

string后面添加memory

function setmeme(string memory _url, string memory _name, uint _timestamp) public{

查阅Solidity 0.5.0的更改内容,请点击https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html


5
//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

对我来说它有效。

  • 字符串内存名字

输入图像描述


1
请选择不同版本的Solidity编译器。对我来说,^0.4.25可行。
Solidity编译器的版本必须在文件和Remix的编译选项卡中设置(它是一个下拉菜单)。

如果我使用另一个编译器,它会给我一个SyntaxError:源文件需要不同的编译器版本(当前编译器为0.5.0+)。 - john mashano makumbi
你需要在文件和Remix上都更改编译器版本。这是在编译选项卡中的下拉菜单。我选择了0.4.25+commit.59dbf8f1,并且您的代码没有任何问题。 - nikos fotiadis
已经在文件和编译器上进行了更改,现在它可以正常工作了。 - john mashano makumbi

0

您需要明确返回参数的内存:

因此,

function setmeme(string _url,string _name, uint _timestamp) public

变成

function setmeme(string memory _url, string memory _name, uint _timestamp) public{

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