繁体   English   中英

Solidity - 如何编写将地址设置为结构的 function?

[英]Solidity - How do I write a function that sets an address to a struct?

我正在尝试创建一个 function 可以为结构设置地址,以便我可以输入该地址并将结构数据返回给我。 即 0x876... 返回名称“John”,余额“99”

我在“getInfoByWallet[_wall] = teamWallets;”上收到一条错误消息说的是

“来自solidity:mapping.sol:22:34:TypeError:类型类型(struct Mapping.teamWallets存储指针)不能隐式转换为预期类型struct Mapping.teamWallets storage ref。getInfoByWallet [_wall] = teamWallets;”

我很困惑,不知道该怎么做。 任何帮助深表感谢。

pragma solidity ^0.5.11;
pragma experimental ABIEncoderV2;

contract Mapping {

    struct teamWallets {
        string name;
        uint256 balance;
    }

    string name = "";
    uint256 balance = 0;
    teamWallets[] public _teamWallets;
    
    mapping(address => teamWallets) public getInfoByWallet;

    function addteamData(string memory _name, uint256 _balance) public {
        _teamWallets.push(teamWallets(_name, _balance));
    }

    function setInfo(address _wall, teamWallets memory) public {
        getInfoByWallet[_wall] = teamWallets;
    }
}

如果你想使用 struct 和 Solidity 映射,那么你必须遵循我在下面提到的这种方法。

在这里,您的 teamWallets 是结构。 那么您已经在映射 getInfoByWallet 时使用了它。

现在,当您必须在映射中设置数据时

struct teamWallets {
    string name;
    uint256 balance;
}

mapping(address => teamWallets) public getInfoByWallet;


function setInfo(address _wall, string memory _name, uint256 _balance) public {
    getInfoByWallet[_wall].name=_name;        
    getInfoByWallet[_wall].balance=_balance;
}

因此,这是将数据存储在结构中并具有可靠映射的正确方法。 我希望,现在你清除了这个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM