简体   繁体   English

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

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

I'm trying to create a function that can set an address to a struct so that I can enter that address and get the struct data returned to me.我正在尝试创建一个 function 可以为结构设置地址,以便我可以输入该地址并将结构数据返回给我。 Ie 0x876... returns name "John", balance "99"即 0x876... 返回名称“John”,余额“99”

I get an error message on 'getInfoByWallet[_wall] = teamWallets;'我在“getInfoByWallet[_wall] = teamWallets;”上收到一条错误消息that says说的是

"from solidity: mapping.sol:22:34: TypeError: Type type(struct Mapping.teamWallets storage pointer) is not implicitly convertible to expected type struct Mapping.teamWallets storage ref. getInfoByWallet[_wall] = teamWallets;" “来自solidity:mapping.sol:22:34:TypeError:类型类型(struct Mapping.teamWallets存储指针)不能隐式转换为预期类型struct Mapping.teamWallets storage ref。getInfoByWallet [_wall] = teamWallets;”

Im pretty confused and not really sure what to do.我很困惑,不知道该怎么做。 Any help is much appreciated.任何帮助深表感谢。

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;
    }
}

If you want to use struct with mapping in solidity then you have to follow this approach that I have mention in below.如果你想使用 struct 和 Solidity 映射,那么你必须遵循我在下面提到的这种方法。

here your teamWallets is struct.在这里,您的 teamWallets 是结构。 then you have used it in mapping getInfoByWallet.那么您已经在映射 getInfoByWallet 时使用了它。

Now, when you have to set the data in mapping like现在,当您必须在映射中设置数据时

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;
}

So, this is the right approach to store a data in struct with mapping in solidity.因此,这是将数据存储在结构中并具有可靠映射的正确方法。 I hope, now you clear with this error.我希望,现在你清除了这个错误。

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

相关问题 如何使用 function 重新分配结构值 - How to reassign a struct value with a function in solidity 如何用Solidity操作Solidity结构? - How to manipulate a Solidity struct with Solidity? 如何在循环中更改New结构的地址? - How do I change the address of a New struct in a loop? 如何在 Solidity 结构中包含(结构的)数组 - How to include Array (of a struct) in a Struct in Solidity C:如何编写搜索函数以在结构数组中查找匹配项,然后返回(打印)它匹配的整个结构? - C: how would I write a search function to look for a match in a struct array, and return (print) the entire struct it matched? 如果我使用结构来承载argc和argv,如何将地址argv分配给结构内的变量? - If I use a struct to carry argc and argv, how do I assign the address argv to a variable inside the structure? 如何获取结构成员的地址 - How can I get the address of struct member 如何使用 function 将字符串添加到结构中? - How do I add a string to a struct using a function? 如何在RubyFFI中将结构指定为函数的返回值? - How do I specify a struct as the return value of a function in RubyFFI? 如何在MATLAB中将结构字段作为函数输出传递? - How do I pass a struct field as a function output in MATLAB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM