3

I am confused why the following code is not working. Any help would be greatly appreciated.

pragma solidity >=0.4.22 <0.6.0

contract InsuranceClaimFactory{
    address public claimer;
    address[] public deployedInsuranceClaim;
    function createInsuranceClaim () public {
        address newInsuranceClaim = new InsuranceClaim(msg.sender);
        deployedInsuranceClaim.push(newInsuranceClaim);
    }

    function getDeployedInsuranceClaims () public view returns (address[] memory){
        return deployedInsuranceClaim;
    }
}

The constructor for InsuranceClaim is

constructor (address creator) public {
claimer=creator;
}

The error is

TypeError: Type contract InsuranceClaim is not implicitly convertible to expected type address

2
  • What behaviour you are expecting with new InsuranceClaim(msg.sender)?
    – Aniket
    Dec 4, 2018 at 11:01
  • It seems like may be 0.4.99 or greater versions change the syntax of a creating object but in a lower version like 0.4.24 this code runs without error. Dec 4, 2018 at 11:23

4 Answers 4

4

ERROR: Type contract InsuranceClaim is not implicitly convertible to expected type address means you can not store InsuranceClaim type of object in address variable. AS per solidity version 0.5.0 Documentation you can define and use InsuranceClaim like this:

pragma solidity>0.4.99<0.6.0;

contract InsuranceClaimFactory{
    address public claimer;
   
    InsuranceClaim[] public deployedInsuranceClaim;
    function createInsuranceClaim () public {
        InsuranceClaim newInsuranceClaim = new InsuranceClaim(msg.sender);
        deployedInsuranceClaim.push(address(newInsuranceClaim));
    }

    function getDeployedInsuranceClaims () public view returns (InsuranceClaim[] memory){
        return deployedInsuranceClaim;
    }
}
1

you can do

address newInsuranceClaim = address(new InsuranceClaim(msg.sender));

and it would work fine

0
  function createInsuranceClaim () public {
        // type is InsuranceClaim
        InsuranceClaim newInsuranceClaim = new InsuranceClaim(msg.sender);
        //                     push the address of new contract
        deployedInsuranceClaim.push(address(newInsuranceClaim));
    }
0

If you are working with version >=0.8, you need:

InsuranceClaim newInsuranceClaim = new InsuranceClaim(msg.sender);
deployedInsuranceClaim.push(payable(address(newInsuranceClaim)));

Note the use of payable here.

1
  • That's only if deployedInsuranceClaim were of type payable[]. In the question it's just an ordinary address.
    – cameel
    Jan 8 at 23:24

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.