Solidity
如何從 Solidity/Remix 中的映射數組中獲取索引?
我可以將工作人員地址推送到與經理地址的映射。我嘗試像這段程式碼一樣獲取經理地址。
contract Test { struct Worker{ address manager_add; address worker_add; } mapping(address => Worker[]) manager; function setWorkerAddress(address worker_add) public{ manager[msg.sender].push(Worker(msg.sender, worker_add)); // push worker address map with manager address } function getManagerAddress() public returns(uint){ return manager[msg.sender].x; // worker get manage address } }
它顯示這樣的錯誤。
TypeError: Return argument type address is not implicitly convertible to expected type (type of first return variable) uint256. --> contracts/test.sol:21:14: | 21 | return manager[msg.sender][index].manager_add; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如何解決?
您正在嘗試查看工人經理的地址。因為您要返回一個地址,所以您必須更改
function getManagerAddress(uint index) public returns(uint)
為function getManagerAddress(uint index) public returns(address)