0

I would like to set array contents using data present in another one. I'm tring to use the function below but got opcode error. Can anyone explain me why?

function getfreecars() public returns(uint) {
      for (uint i=0;i<car.length; i++){
          if(car[i].status == 0){
              freecars.push(Car({
                        car_name:car[i].car_name,
                        plate:car[i].plate,
                        status:car[i].status
              }));
          }
          else{}
      }
      return freecars.length;
      
  }

I leave also complete code:

pragma solidity ^0.6.0;

contract new_client {
    enum carstatus {free, rented, maintenance}
    enum isclient {client, not_client}
    uint clientstatus;
    uint public refclient;

    struct Car {
        string car_name;
        string plate;
        uint status;
    }
    
    mapping(address => Car) booking;
    Car[] public car;
    Car[] public freecars;
     uint c=0;
    
    struct Client {
        string name;
        string surname;
        uint age;
        bool license;
        address id;
    
    }
    
    uint total_client;
    
    Client[] public client;
    uint numberofclient;

    
    function innewclient(string memory name_, string memory surname_, uint  age_, bool  license_, address id_) public  {
        require(age_ >= 18, "must be 18 at least");
        require(age_ <= 75, "can't be over 75");
        
        
        client.push(Client({
            name: name_,
            surname: surname_,
            age: age_,
            license: license_,
            id: id_
            
        }));
        
        numberofclient = client.length;
        
    }
                                
   function gettotalclient() public view  returns(uint) {
        
       return numberofclient;
   }
    
    function gettotalclient2() public view  returns(uint) {
        
       return client.length;
   }
    function checkifclient(address _id) public view returns(string memory,string memory,uint ,bool ,address) {
        for (uint i=0; i<client.length;i++ ) {
            if (client[i].id ==_id){
                return (client[i].name,client[i].surname,client[i].age,client[i].license,client[i].id);
            }
            else {}
        }
        
       // return refclient;
     
    }
    
  function insertnewcar(string memory model, string memory plate_, uint  status_) public  {
    
     car.push(Car({
         car_name:model,
         plate:plate_,
         status: status_
     }));
     

  }
  
  function gettotalcar() public view returns(uint) {
      return car.length;
  }
  
  
  function carlist() public view returns(string memory,string memory,uint) {
      
      for (uint i=0; i<car.length; i++){
          
          return (car[i].car_name,car[i].plate,car[i].status);
      }
  }
  
  function getfreecars() public returns(uint) {
      for (uint i=0;i<car.length; i++){
          if(car[i].status == 0){
              freecars.push(Car({
                        car_name:car[i].car_name,
                        plate:car[i].plate,
                        status:car[i].status
              }));
          }
          else{}
      }
      return freecars.length;
      
  }
  
  
  
}
4
  • The compiler does not issue any errors. However, it should be noted that, firstly, there is no initialization of the freecars array in the function, and, secondly, the use of iterative iteration over an array of indefinite size is bad practice for Solidity – Mad Jackal Jan 30 at 15:27
  • 1
    I tried the contract on remix and it works as expected. How are you testing? Are you using ganache or a private testnet? Does the testnet have all opcodes enabled? What's solc version? What's the exact error message? – Ismael Jan 31 at 5:29
  • Why is iteration over an array of indefinite size bad practice for solidity? – Gabriele Jan 31 at 9:41
  • @Gabriele because if the array is long enough, your transaction will run into an out-of-gas error, or the required gas will be greater than the gas limit for the full block – Franco Victorio Jan 31 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.