0
pragma solidity >=0.4.0 <0.7.0;

contract InsanFactory{

    struct User{
         string name;
         address myaddress;
    }

    mapping(address => User) public userStructs;

    address[] public userAddress ;

    function createUser(string memory name,address myaddress) public{
        userStructs[msg.sender] = name;
        //set user name
        userStructs[msg.sender] = myaddress;
        //address'i degistirdim
        userAddress.push(msg.sender);

    }
    function getResult() external view returns(address[] memory){
        return userAddress;
    }
}

When I compile this code I got 2 errors:

First error at line 15 says:

Type string memory is not implicitly convertible to expected type struct InsanFactory.User storage ref.
line 15(userStructs[msg.sender] = name;)

Second one at line 17 says:

Type address is not implicitly convertible to expected type struct InsanFactory.User storage ref.
line 17(userStructs[msg.sender] = name;)

What should I do for my error ?

1

You need to directly access the members of the struct. Solidity docs.

userStructs[msg.sender].name = name;

userStructs[msg.sender].myaddress = myaddress;

Code:

pragma solidity >=0.4.0 <0.7.0;

contract InsanFactory{
    struct User{
        string name;
        address myaddress;
    }
    mapping(address => User) public userStructs;
    address[] public userAddress ;

    function createUser(string memory name,address myaddress) public{
        userStructs[msg.sender].name = name;
        //set user name
        userStructs[msg.sender].myaddress = myaddress;
        //address'i degistirdim
        userAddress.push(msg.sender);
    }
    function getResult() external view returns(address[] memory){
        return userAddress;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.