Solidity

Solidity - 結構數組內部映射 |如何引用結構數組元素?

  • March 23, 2022

我無法引用映射內部的結構元素。

映射:

   struct VoterData {
       string iD;
       address voterEthAddress;
   }

   // One to Many mapping; one Eth Address can have many VoterData
   mapping(address => VoterData[]) public votersMapping;

這是我試圖引用映射的程式碼:

emit messageEvent("Voter Address: ",votersMapping[localAddress].voterEthAddress );

錯誤

TypeError: Member "voterEthAddress" not found or not visible after argument-dependent lookup in struct Voter.VoterData storage ref[] storage ref.
--> contracts/Voter.sol:64:53:
|
64 | emit messageEvent("Voter Address: ",votersMapping[localAddress].voterEthAddress );
|

您有一個指向結構數組的地址映射。

您缺少數組的索引。(也許他們可以對不同的提案進行投票,這就是為什麼你有那個輔助數組並且索引是提案 ID?雖然這看起來效率低下)

mapping(address => VoterData[]) public votersMapping;
emit messageEvent("Voter Address: ",votersMapping[localAddress][index].voterEthAddress );

應該可以,但是我想您不希望該索引在那裡,並且您希望地址直接指向選民數據,您應該修改地圖以不指向數組。

mapping(address => VoterData) public votersMapping;

祝你好運!

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