1

Doing lesson 5 from here how to write smart-contract, lesson 5 I got compile error in remix. It goes:

browser/ballot.sol:40:2: ParserError: Expected pragma, import directive or contract/interface/library definition. function approve(address _spender, uint _value) public constant returns (bool success){

and the full code is:

pragma solidity ^0.4.18;

contract SimpleTokenCoin{

string public constant name = "Simple Coin Token";

string public constant symbol = "SCT";

uint32 public constant decimals = 18;

uint public totalSupply = 0;

mapping (address => uint) balances;

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

function transfer(address _to, uint _value) public returns (bool success){ if(balances[msg.sender] >= _value && balances[_to] + _value >= balances[_to]){ balances[msg.sender] -=_value; balances[_to] += value; Transfer(msg.sender, _to, _value); return true; } return false; }

function transferFrom(address _from, address _to, uint _value) public returns (bool success){ if(balances[_from] >= _value && balances[_to] + _value >= balances[_to]){
balances[_from] -=_value; balances[_to] += value; Transfer(_from, _to, _value); return true; } return false; } }

function approve(address _spender, uint256 _value) public constant returns (bool success){ return false; }

function allowance(address _owner, address _spender) public constant retuns (uint256 remaining){ return 0; }

function mint(address _to, uint _value) public { assert(totalSupply + _value >= totalSupply && balances[_to] +_value >= balances[_to]); balances[_to] += _value; totalSupply += _value;
}

event Transfer(address indexed _from, address indexed _to, uint value);

event Approval(address indexed _owner, address indexed _spender, uint _value); 
} 
  • The second guess in my answer is correct. I think that if you format your code properly, you'll see the issue. Make sure you indent every time you have an open curly brace and unindent when you have a closing curly brace. – user19510 Mar 2 '18 at 5:00
0

You'd have to share your full code for us to know for sure, but my guess is that you're not defining a contract. Your code should look something like this:

contract MyContract {
    function approve(address _spender, uint256 _value) public constant returns (bool success) {
        return false;
    }
}

It could also be that you closed the contract too early. In general, the error probably means you tried to define a function outside of a contract.

0

You are missing an r on the returns part of the allowance function

I would also suggest for future problems like this use an IDE like Remix to spot syntax and almost all other errors (excluding logic errors of course)

  • I use remix yes – Anatoly Mar 5 '18 at 5:05
  • I accepted another one by smarx, it was made earlier and right at the point – Anatoly Mar 5 '18 at 10:09

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.