2

Going through some tutorials on ethreum, and got stuck here. The code seems ok for me. I tried to add the public/private as the error says, then i got another error like:

parsererror function variable struct or modifier declaration expected  

Who could help me out ?

pragma solidity ^0.5.0;

contract MyFirstContract{
    string private name;
    uint private age;

    function setName(string newName){
        name = newName;
    }

   function getName()returns (string){
        return name;
    }

    function setAge(uint newAge){
        age = newAge;
    }

    function getAge()returns (uint){
        return age;
    }
}

errors
errors

EDIT:
WHo could siply explain why this code doesn't display the name ? the function getName should've returned "jack"-string, but its blank. Also i'm using Java Script Environment edited

0

As per the Solidity v0.5.0 breaking changes here:

Explicit function visibility is now mandatory. Add public to every function and constructor, and external to every fallback or interface function that does not specify its visibility already.

Your functions are missing visible specifier. Regarding the data location, it says:

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.

When they mention array, it also applies on bytes ans string. Make the related changes and errors will be gone.

2

Before 0.5.0, solidity by default sets formal parameters to Memory. Now in 0.5.0, You need to mention if its memory or storage variable.

Heres solution of your code.

pragma solidity ^0.5.0;

contract MyFirstContract{
    string private name;
    uint private age;

    function setName(string memory newName)
    public {
       name = newName;
    }

   function getName()
   view
   public
   returns (string memory) {
        return name;
    }

    function setAge(uint newAge)
    public {
        age = newAge;
    }

    function getAge()
    view
    public
    returns (uint) {
        return age;
    }
}
  • heres second point about changes in Solidity Formal Parameters. – faran ahmed Nov 27 '18 at 4:58
  • Do you know why my code,(also your code) doesn't displays the name in getName() function. ?? i've edited the question and posted the screenshot up there . – Stanley Nov 27 '18 at 6:21
  • you must Execute/Transact function setName with parameters in double quotes, it should be "jack". Then call getName(). – faran ahmed Nov 27 '18 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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