Compilation

為地址的公共映射編譯 TypeError ==> uint?

  • October 25, 2018

也許這真的很明顯,但由於某種原因,我無法擺脫這個錯誤。在編譯時,我收到以下錯誤:

TypeError: Indexed expression has to be a type, mapping or array (is uint256)
       voteType[msg.sender] = 2;
       ^------^
Compilation failed. See above.

索引表達式是到類型 ( uint) 的映射,應該都很好。

相關程式碼:

function castVote (uint voteType) canVote {
//        require (voteType[msg.sender] == 0);
       if (voteType == 2) {//2 here meaning "Real" account.
       count[upForVote].voteReal++;
       voteType[msg.sender] = 2;
       }
       if (voteType == 1) {//1 here meaning "Fake" acount.
       count[upForVote].voteFake++;
       voteType[msg.sender] = 1;
       }
       voteInProgress[msg.sender] = true;
   }

您的函式接受一個名為 的參數voteType,它是一個uint. 所以你不能索引它,因為它不是 aarray或 a mapping

我的猜測是,您在其他地方聲明了一個狀態變數,也稱為voteType,但它被此函式中的參數名稱所掩蓋。

在這些情況下,一種約定是使用前導下劃線來區分,儘管這通常適用於兩個變數具有相同類型的情況:

function castVote (uint _voteType) canVote {
   if (_voteType == 2) {  //2 here meaning "Real" account.
       count[upForVote].voteReal++;
       voteType[msg.sender] = 2;
   }
   if (_voteType == 1) {  //1 here meaning "Fake" acount.
       count[upForVote].voteFake++;
       voteType[msg.sender] = 1;
   }
   voteInProgress[msg.sender] = true;
}

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