Solidity

在映射中傳遞列舉值

  • April 19, 2022

我試圖傳遞一個預設值,但得到以下錯誤: "Invalid implicit conversion from enum tlb.Category to enum tlb.Category[] memory requested"

有人知道如何解決這個問題嗎?


   uint256 public pageNo = 0;
   enum Category {DEFAULT, I, II, IIIA, IIIB}
   mapping(uint => TLB) public TLBs;
   
   struct TLB{
       uint flightNo;
       string flightFrom;
       string flightTo;
       uint256[3] reportDate;
       Category[] category;
       bool EtopsFlight;
       string maintCheck;
       uint licenseNo;
       uint station;
       bool CaaCertification;
       bool handlingAgent; 
       uint CertNo;
   }
   
   function addTLB(uint _flightNo, string memory _flightFrom, string memory _flightTo, uint256[3] memory _reportDate) public {
       Category _category = Category.DEFAULT;
       
       TLBs[pageNo] = TLB(_flightNo, _flightFrom, _flightTo, _reportDate, _category, false, "", 0, 0, false, false, 0);
   }

您的結構需要一個 Category 數組,Category[] category;而您只傳遞了一個列舉值。如果您只需要傳遞一個值,請將結構變數更改為Category category;,它將正常工作。

您想將Enum類型的值傳遞Category給您的TLBs地圖,因此您必須categorystruct TLBas中聲明Category

Category[] category; 必須如下 Category category;

引用自:https://ethereum.stackexchange.com/questions/97222