[Ethereum] Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires “view”

contract-debuggingsolidity

I can't work out how to modify this…When I change pure to view I get a different error:

function balanceOf(address _owner) public pure returns (uint256 balance) {
    return balances[_owner];
}

changing to view gives this error:

Overriding function changes state mutability from "pure" to "view".
    function balanceOf(address _owner) public view returns (uint256 balance) {
    ^
Spanning multiple lines.

Best Answer

You're fine. Because you're reading a state variable with Blockchain data (ie the reserved keywords like tx, msg, and so on) it's giving you a compiler warning as the compiler is not smart enough to understand that you're not actually modifying anything.

Also make sure getCoinAge is also marked view. http://solidity.readthedocs.io/en/develop/miscellaneous.html?highlight=Pure

Related Topic