I tried solidity example like as above in remix, solidity version > 0.5.0 But I am getting this error now. What is the way to solve this error?
contract MyContract { string value; function get() public view returns (string) { return value; } function set(string _value) public { value = _value; } constructor() public { value = "myValue"; } }
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.
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.
Now, with the new versions of solidity you can return a struct also by using memory keyword after writing struct name in return type.
To define a structure struct keyword is used, which creates a new data type. For accessing any element of the structure, 'dot operator' is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.
You should add memory keyword for string parameter, which was introduced in solidity version 0.5.0
As per the documentation:
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. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[][] memory x) where memory is the data location and might be replaced by storage or calldata accordingly. Note that external functions require parameters with a data location of calldata.
Corrected code
contract MyContract { string value; function get() public view returns (string memory) { return value; } function set(string memory _value) public { value = _value; } constructor() public { value = "myValue"; } }
Refer to official documentation on breaking changes made in version 0.5.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With