0

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. I.e. 0x876... returns name "John", balance "99"

I get an error message on '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;"

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

1 Answer 1

0

If you want to use struct with mapping in solidity then you have to follow this approach that I have mention in below.

here your teamWallets is struct. then you have used it in mapping 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.