Addresses

地址數組的問題

  • January 26, 2019

我有以下智能合約程式碼,它有一個簡單的問題:

browser/Referrals.sol:100:35: TypeError: Type function (uint256) 純返回(地址

$$ $$memory) 不能隱式轉換為預期的類型地址$$ $$儲存參考 推薦人$$ msg.sender $$= 新地址$$ $$;

你能解釋一下我該如何解決這個問題。

library SafeMath 
{
   function add(uint a, uint b) internal pure returns (uint c)
   {
       c = a + b;
       require(c >= a);
   }
   function sub(uint a, uint b) internal pure returns (uint c) 
   {
       require(b <= a);
       c = a - b;
   }
   function mul(uint a, uint b) internal pure returns (uint c) 
   {
       c = a * b;
       require(a == 0 || c / a == b);
   }
   function div(uint a, uint b) internal pure returns (uint c)
   {
       require(b > 0);
       c = a / b;
   }
}

contract Owned {
   address public owner;
   address public newOwner;

   event OwnershipTransferred(address indexed _from, address indexed _to);

   constructor() public {
       owner = msg.sender;
   }

   modifier onlyOwner {
       require(msg.sender == owner);
       _;
   }

   function transferOwnership(address _newOwner) public onlyOwner {
       newOwner = _newOwner;
   }
   function acceptOwnership() public {
       require(msg.sender == newOwner);
       emit OwnershipTransferred(owner, newOwner);
       owner = newOwner;
       newOwner = address(0);
   }
}

contract ReferraledToken is Owned
{
   using SafeMath for uint;
   string public symbol;
   string public  name;
   uint8 public decimals;
   uint _totalSupply;
   uint64 current = 100000;
   mapping(address => uint) balances;
   mapping(address => address) ReferralOf;
   mapping(address => address[]) ReferralsOf;
   mapping(address => uint64) ReferralLink;
   mapping(uint64 => address) LinkOf;
   mapping(address => bool) registeredUser;

   constructor() public {
       symbol = "LETO";
       name = "IT project";
       decimals = 5;
       _totalSupply = 1000000 * (10 ** uint(decimals));
       balances[owner] = _totalSupply;
   }

   function totalSupply() public view returns (uint)
   {
       return balances[owner];
   }
   function balanceOf(address tokenOwner) public view returns (uint balance)
   {
       return balances[tokenOwner];
   }
   function transfer(address to, uint tokens) public returns (bool success) {
       balances[msg.sender] = balances[msg.sender].sub(tokens);
       balances[to] = balances[to].add(tokens);
       return true;
   }
   function sendPurchaseToken(address to) payable public returns (bool) 
   {
       balances[owner] = balances[owner].sub(uint(decimals) * msg.value / 10);
       balances[to] = balances[to].add(uint(decimals) * msg.value / 10);
       return true;
   }
   function registerUser(uint64 _code) public
   {
       require(registeredUser[msg.sender] == false);
       ReferralOf[msg.sender] = LinkOf[_code]; 
       ReferralsOf[LinkOf[_code]].push(msg.sender);
       ReferralLink[msg.sender] = current;
       LinkOf[ReferralLink[msg.sender]] = msg.sender;
       ReferralsOf[msg.sender] = new address[];
       current += 1;
   }
   function lengthOfPath(address _start, uint _counter) public returns (uint)
   {
       if (ReferralOf[_start] != owner)
       {
           return lengthOfPath(ReferralOf[_start], _counter + 1);
       }
       else
       {   
           return _counter;
       }
   }

}

new address[]在記憶體中返回一個新的地址數組。然後您不能將其分配給,ReferralsOf[msg.sender]因為沒有對此類數組複製操作的內置支持。

幸運的是,您可能根本不需要這樣做。您可能打算設置registeredUser[msg.sender]true該函式中的某個位置。如果你這樣做了,你就不必擔心重用ReferralsOf[msg.sender],所以沒有必要做任何事情。(該結構中的數組已經為空。)

但是,如果您想要雙重安全,您可以使用delete ReferralsOf[msg.sender]orReferralsOf[msg.sender].length = 0清空數組(如果其中已經有數據)。

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