2

I started my first code in solidity . when i was implemented getter and setter i have face some error .

The Following code,

pragma solidity ^0.5.0;

contract MyFirstContract{
    string private name;
    uint private age;

    function setName(string newName) public{
        name=newName;
    }
    function getName()public view returns(string){
        return name;
    }

    function setAge(uint newAge) public{
        age=newAge;
    }
    function getAge()public view returns(uint){
        return age;
    }

}

Error 1:

browser/MyFirstContract.sol:7:22: TypeError: Data location must be "memory" for parameter in function, but none was given.
    function setName(string newName) public{
                     ^------------^

Error 2:

browser/MyFirstContract.sol:10:43: TypeError: Data location must be "memory" for return parameter in function, but none was given.
    function getName()public view returns(string){
                                          ^----^
0

1 Answer 1

2

You need to give memory keyword to your code function string parameter like this:

function setName(string memory newName) public{

and also for return string parameter

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

4
  • Thank you . Can you suggest some tutorial where i can learn blockchain related video tutorial Commented Dec 14, 2018 at 8:56
  • If you want to learn solidity from the beginning then watch this tutorial series link and latest changes from link. If you want to learn about blockchain than you get many videos on the internet and then you have to decide that with which platform(ethereum,hyperledger,eosio,etc..) you want to go for development. Commented Dec 14, 2018 at 9:05
  • It’s a breaking change that was introduced in 0.50. If you use a lower version this should work
    – 0xsegfault
    Commented Dec 14, 2018 at 13:31
  • @MaheshRajput Perfect already i started to follow that tutorial only Commented Dec 15, 2018 at 0:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.