[Ethereum] Solidity: ParserError: Expected pragma, import directive or contract/interface/library definition. uint private age; ^

atomcompilerremixsolidity

I am using Atom together with etheratom and I don't get why this simple code won't run:

pragma solidity ^0.4.19

contract MyFirstContract {
    string private name;
    uint private age;

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

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

It shows me the following error:

:5:5: ParserError: Expected pragma, import directive or contract/interface/library definition. uint private age; ^

Anybody an idea whats wrong?

Best Answer

You're missing a ; after declaring what version of solidity you're using. Use this pragma solidity ^0.4.19; in the first line instead. Consider using remix IDE in the future to help solve these small errors.

You can also use view on the getName function, this can be used as the function only reads data and will not cost Ether to run. Here is the updated function

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

updated code

pragma solidity ^0.4.19;

contract MyFirstContract {
    string private name;
    uint private age;

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

function getName() view returns (string) {
    return name;
    }
}
Related Topic