Solidity

使用 truffle compile 編譯時未聲明的標識符

  • July 6, 2017

我有這份契約,但無法弄清楚問題出在哪裡。有任何想法嗎?(我是一個堅固的菜鳥,幾天前才開始)。

HelloWorld.sol 文件:

mapping (address => uint) balances;

function HelloWorld() {
   balances[msg.sender] = 1000;
}

function getBalance() public constant returns(uint) {
   return balances;
}

function sendCoin(address receiver, uint amount) public constant returns(bool sufficient) {
   if (balances[msg.sender] < amount) return false;

   balances[msg.sender] -= amount;
   balances[receiver] += amount;
   return true;
}

Moon.sol 文件:

mapping (address => uint) balances;

function Moon () {
   balances[tx.origin] = 1000;
}   

function getBalance() public constant returns(uint) {
   return balances;
}

在 Moon.sol 文件中,我錯過了“映射(地址 => uint)餘額;”。

松露編譯器錯誤:

$ truffle compile
Compiling .\contracts\HelloWorld.sol...
Compiling .\contracts\Moon.sol...

/C/Users/guerr/SmartContracs/firstPorject/contracts/Moon.sol:10:3: : 
Undeclared identifier.
           balances[tx.origin] = 1000;
           ^------^
Compiliation failed. See above.

您的程式碼看起來不錯,但出於安全原因您應該更改tx.origin為。幾乎總是一個錯誤。msg.sender``tx.origin

我會擺脫那個moon.sol文件,然後再試一次。

引用自:https://ethereum.stackexchange.com/questions/19572