Struct

未找到結構參數

  • November 22, 2018

我正在嘗試在結構中呼叫函式 insertOrigin,然後呼叫其他函式 getOrigin 以返回該值。但是 VS Code 出現錯誤

pragma solidity ^ "0.4.24";

contract Origin {

struct OriginInfo
{
   string airportName;
   uint32 checkInTime; //In timestamp
   uint8 bagWeight;
   address clientAddress;
   bool itsHere;
}

function insertOrigin(string _airportName, uint32 _checkInTime, uint8 _bagWeight, bool _itsHere) external
{
   /*OriginInfo[] originInfoArray;
   originInfoArray.length++;

   originInfoArray[originInfoArray.length-1].airportName = _airportName;
   originInfoArray[originInfoArray.length-1].checkInTime = _checkInTime;
   originInfoArray[originInfoArray.length-1].bagWeight = _bagWeight;
   originInfoArray[originInfoArray.length-1].clientAddress = _clientAddress;
   originInfoArray[originInfoArray.length-1].itsHere = _itsHere;*/


   OriginInfo(_airportName, _checkInTime, _bagWeight, msg.sender, true);
   getOrigin();
}

function getOrigin() private constant returns (string, uint32, uint8, address, bool) 
{
   return (OriginInfo.airportName, 
   OriginInfo.checkInTime, 
   OriginInfo.bagWeight, 
   OriginInfo.clientAddress, 
   OriginInfo.itsHere);
}
}

在此處輸入圖像描述

任何的想法?

您不是在查詢 的實例OriginInfo,而是在查詢 struct 聲明本身。您必須使用映射才能實現您想要的:

pragma solidity ^ "0.4.24";

contract Origin {

   struct OriginInfo
   {
       string airportName;
       uint32 checkInTime; //In timestamp
       uint8 bagWeight;
       address clientAddress;
       bool itsHere;
   }
   mapping (uint256 => OriginInfo) origins;
   uint256 public originNonce;

   constructor() public {
       originNonce = 1;
   }

   function insertOrigin(string _airportName, uint32 _checkInTime, uint8 _bagWeight, bool _itsHere) 
       external 
   {
       origins[originNonce] = OriginInfo(_airportName, _checkInTime, _bagWeight, msg.sender, true);
       originNonce += 1;
   }

   function getOrigin(uint256 _originId) 
       private 
       constant 
       returns (string, uint32, uint8, address, bool) 
   {
       OriginInfo memory origin = origins[_originId];
       return (
           origin.airportName,
           origin.checkInTime, 
           origin.bagWeight, 
           origin.clientAddress, 
           origin.itsHere
       );
   }
}

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