Solidity

類型字元串記憶體不能隱式轉換為預期類型 struct InsanFactory.User storage ref

  • June 27, 2019
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;
   }
}

當我編譯這段程式碼時,我得到了 2 個錯誤:

第 15 行的第一個錯誤說:

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

第 17 行的第二個說:

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

我應該為我的錯誤做些什麼?

您需要直接訪問結構的成員。Solidity 文件

userStructs[msg.sender].name = name;

userStructs[msg.sender].myaddress = myaddress;

程式碼:

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

引用自:https://ethereum.stackexchange.com/questions/72307