Ethereum-Wallet-Dapp
混音中的編譯錯誤並不能說明問題
從這裡開始第 5 課如何編寫智能合約,第 5 課我在混音中遇到編譯錯誤。它去:
browser/ballot.sol:40:2: ParserError: Expected pragma, import directive or contract/interface/library definition。函式批准(地址_spender,uint_value)公共常量返回(布爾成功){
完整的程式碼是:
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); }
您必須分享您的完整程式碼才能讓我們確定,但我的猜測是您沒有定義契約。您的程式碼應如下所示:
contract MyContract { function approve(address _spender, uint256 _value) public constant returns (bool success) { return false; } }
也可能是您過早關閉了契約。一般來說,這個錯誤可能意味著你試圖在合約之外定義一個函式。
您缺少功能
r
的returns
一部分allowance
我還建議像這樣的未來問題使用像 Remix 這樣的 IDE 來發現語法和幾乎所有其他錯誤(當然不包括邏輯錯誤)