1

I have following code of my smart contract, it has simple problem:

browser/Referrals.sol:100:35: TypeError: Type function (uint256) pure returns (address[] memory) is not implicitly convertible to expected type address[] storage ref. ReferralsOf[msg.sender] = new address[];

Can you explain how can I solve this problem.

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

}

1 Answer 1

1

new address[] returns a new array of addresses in memory. You can't then assign that to ReferralsOf[msg.sender] because there's no built-in support for such an array copy operation.

Fortunately, you probably don't need to do it at all. You probably meant to set registeredUser[msg.sender] to true somewhere in that function. If you do that, you don't have to worry about reusing ReferralsOf[msg.sender], so there's no need to do anything. (The array in that struct is already empty.)

But if you want to be doubly safe, you can use delete ReferralsOf[msg.sender] or ReferralsOf[msg.sender].length = 0 to empty the array if it already has data in it.

Your Answer

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

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