Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Data location must be "memory" for parameter in function, but none was given

Tags:

solidity

I tried to compile my code, but I got the following error:

TypeError: Data location must be "memory" for parameter in function, but none was given

my code:

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;   
    }
}
like image 639
john mashano makumbi Avatar asked Nov 24 '18 17:11

john mashano makumbi


People also ask

What is memory keyword in Solidity?

memory is a keyword used to store data for the execution of a contract. It holds functions argument data and is wiped after execution. storage can be seen as the default solidity data storage. It holds data persistently and consumes more gas.

What is data location in Solidity?

In Solidity, all of the state variables and local variables have a data location specified by default, or you can explicitly define a chosen data location. These data locations are called memory and storage. You can override the default data location by explicitly specifying along with the declaration of the variable.

Is String reference type in Solidity?

There are several reference data types in Solidity: fixed-sized arrays, dynamic-sized arrays, array members, byte arrays, string arrays, structs, and mapping.


2 Answers

Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables.

Add memory after string

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

check here for Solidity 0.5.0. changes https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html

like image 78
Yegor Zaremba Avatar answered Sep 22 '22 23:09

Yegor Zaremba


//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;
 }
}
like image 30
Kartik ganiga Avatar answered Sep 19 '22 23:09

Kartik ganiga