1

I am trying to compile the following program. I have declared a structure and I am passing a variable of it in a function in order to assign it to an array of structure.

pragma solidity ^0.5.1; 
contract investorPayment{
   struct st_investors{
      uint invested;
      uint payout;
      address payable Address;
   }
   st_investors[100] investors;
   

   function pay(uint min_investment, address payable addr, st_investors memory newInvestor) public{
      uint payout;
      for (uint i=0; i < investors.length; i++) {
         investors[i].Address = addr;
         if (investors[i].invested == min_investment) {
            payout = investors[i].payout;
            if (!(investors[i].Address.send(payout)))
               revert();
            investors[i] = newInvestor;
         }
      }
   }
}     

I am getting the following error message:'

solc failedsend.sol failedsend.sol:11:60: Error: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature. function pay(uint min_investment, address payable addr, st_investors memory newInvestor) public
^-----------------------------^

Somebody please guide me.

Zulfi.

1

You have two alternatives:

=> Until pragma version ^0.7, you should add the following line:

pragma solidity ^0.5.1;
pragma experimental ABIEncoderV2;  // Add line

=> Upgrade contract to version ^0.8:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
3
  • Is there any other way to code the contract to avoid this type of parameter passing? – zak100 Mar 10 at 23:40
  • 1
    You could pass the 3 fields of struct 'st_investors' as function parameters, and then assign them inside the function: investors[i] = st_investors(field1, field2, field3); – Sergi Juanati Mar 11 at 4:40
  • 1
    By the way, in terms of naming convention, it is recommended to use CapWords style for struct names, in order to differentiate them from variables. So you could use something like 'StInvestors'. – Sergi Juanati Mar 11 at 4:43

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.