Solidity

totalSupply() Solidity 簽名導致衝突錯誤

  • September 29, 2017

我正在 Mist 錢包中使用solidity 編寫一個非常基本的ERC20 代幣。由於某種原因,編譯器不喜歡 totalSupply() 函式名。如果我更改此名稱,它編譯得很好。

為了符合 ERC20,這個函式必須以這種方式簽名。

如何解決此錯誤?

Solidity 程式碼

在此處輸入圖像描述

我認為你的函式返回參數應該被重命名——這是編譯器真正抱怨的:

function totalSupply() constant returns (uint256 something) {

在這裡命名返回參數實際上是可選的;你可以把它的類型:

function totalSupply() constant returns (uint) {

$$ For future reference, it would be easier to check if could post the code itself rather than an image of the code so we can cut and paste into the compiler. $$

選項1

ERC20 代幣的典型修復和實現是將public變數重命名為totalSupply(無下劃線)並刪除函式totalSupply,因為它將由 Solidity 自動生成。

陰影錯誤資訊的解釋

function totalSupply() constant returns (uint256 totalSupply)向編譯器指示返回值將是可變的totalSupply(不帶下劃線)。

但是該函式正在返回另一個變數的值(看起來相同,但不同,因為它有一個下劃線)。所以編譯器錯誤是因為該totalSupply變數被另一個變數覆蓋/隱藏。

選項 2

正如@benjaminion 解釋的那樣,不要命名返回值,只需使用:

function totalSupply() constant returns (uint256)

現在沒有變數可以陰影。

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