1

while writing a smart contract a keep having an error in solidity ^0.8.0 about a nested mapping in a structure, here is my code snippets :

     struct Airline {
     address airlineAddress;
     AirlineState state;
     string name;
     mapping(address => bool) approvals; // I guess this what cause the error
     uint8 approvalCount;
   }
constructor()
    {
      contractOwner = msg.sender;

      airlines[contractOwner] = Airline(contractOwner, AirlineState.Paid, "First Airline", 0); // This an error
      totalPaidAirlines++;
    }
2
  • Could you provide the error that you get, this will make it easier to help you solve the problem.
    – Richard
    Dec 23, 2021 at 8:42
  • Yes, I put it in the subject, but the complete error msg appears like this : "Struct containing a (nested) mapping cannot be constructed."
    – Yaa_seen
    Dec 23, 2021 at 22:19

1 Answer 1

2

Mappings are only stored in storage. By initiating a struct in the constructor you assign it to memory. You should do it like this instead:

   struct Airline {
        address airlineAddress;
        AirlineState state;
        string name;
        mapping(address => bool) approvals; 
        uint8 approvalCount;
   }

   address contractOwner;
   mapping(address => Airline) airlines;
   uint8 totalPaidAirlines;
   constructor()
   {
      contractOwner = msg.sender;
      Airline storage airline = airlines[contractOwner]; //accessing the struct that is already in storage
      //one by one assigning the properties
      airline.airlineAddress = contractOwner;
      airline.state = AirlineState.Paid;
      airline.name = "First Airline";
      airline.approvalCount = 0; 
      totalPaidAirlines++;
   }
1
  • Thanks a lot dude your code work fine :)
    – Yaa_seen
    Dec 23, 2021 at 22:17

Your Answer

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

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