2
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 ?

2 Answers 2

3

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

Your mapping requires a key of address type to value of User type. name is of string memory type and cannot be implicitly converted to User type. Here's a quick fix:

userStructs[msg.sender] = User(name, 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{
//map address type to User type
        userStructs[msg.sender] = User(name, myaddress);
        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 and acknowledge you have read our privacy policy.

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