Solidity
租車系統編譯錯誤
我正在為汽車租賃系統編寫程式碼,但我收到一些不理解的編譯錯誤消息。
第一個錯誤是
TypeError:表達式必須是左值。
第二個是
TypeError:類型地址不能隱式轉換為預期類型函式()查看外部返回(地址)。
第三個是
TypeError: Operator < 不兼容類型 uint256 和 function () 查看外部返回 (uint256)。
這是我的程式碼:
請注意:星號代表程式碼錯誤的哪一行。
pragma solidity ^0.4.18; contract rentCar { struct Renter { address addr; uint DOB; uint currentRenting; } bool public rented; address public owner; uint public duration; uint public rentalPrice; uint public charge; uint public rentalDate; uint public returnDate; uint public rentalStartDate; uint public rentalEndDate; uint public constant totalDays = 7; struct NameKey{ // storage the name's keys uint[] keys; } //List of Cars available uint[] private ids; //Use it to return the ids of Objects uint public numofCars; mapping(uint => Car) private cars; mapping(string => NameKey) private nameToKeys; //Events event E_addCar(uint objID, address VechileOwner); event E_Rent(address indexed _renter, uint _rentalDate, uint _returnDate, uint _rentalPrice); event E_ReturnRental(address indexed _renter, uint _returnDate); //Modifiers modifier onlyOwner() { require(msg.sender == owner); _; } /* modifier onlyRenter() { require(msg.sender == addr); _; } */ modifier whenNotRented() { require(!rented); _; } modifier whenRented() { require(rented); _; } //Funcions function rentCar() public{ owner = msg.sender; } function addCar(string make, string model, uint pricePerDay, uint minRentalDay, uint maxRentalDay, bool available) public onlyOwner { Car newCar = cars[numofCars]; ///nameToKeys[name].keys.push(numofCars); //add the key to the name's keys ///***ERRORS newCar.VechileOwner = msg.sender; newCar.make = make; newCar.model = model; newCar.available = available; newCar.pricePerDay = pricePerDay; newCar.minRentalDay = minRentalDay; newCar.maxRentalDay = maxRentalDay; newCar(numofCars,msg.sender); ids.push(numofCars); numofCars++; } function setAvailable(uint objID, bool _available) public view onlyOwner { ///***ERRORS cars[objID].available = _available; } /* function totalDays (uint rentalStartDate, uint rentalEndDate) public whenNotRented{ uint totalDays = ; return totalDays; }*/ function Rent(uint objID,uint totalDays) public payable whenNotRented returns(bool){ ///***ERRORS require (msg.value < cars[objID].deposit); require(totalDays >= cars[objID].minRentalDay && totalDays <= cars[objID].maxRentalDay); cars[objID].renter = Renter({addr:msg.sender, currentRenting:now}); uint PayDeposit = msg.value - cars[objID].deposit; rentalPrice = totalDays * cars[objID].pricePerDay; rentalDate = rentalStartDate; returnDate = rentalEndDate; cars[objID].available = false; rented = true; // accessCar(); ///***ERRORS E_Rent(Renter, rentalDate, returnDate, rentalPrice); return true; } function endRent (uint objID, uint duration) public whenRented { ///***ERRORS duration = (now - cars[objID].renter.currentRenting) / (24*60*60*1.0); charge = duration * cars[objID].priceDaily - cars[objID].deposit; uint totalPayment = msg.value - charge; require(!cars[objID].VechileOwner.send(charge)); require(!cars[objID].renter.addr.send(charge)); delete cars[objID].renter; cars[objID].available = false; E_ReturnRental(Renter, now); resetRental(); } function forceRentalEnd() public onlyOwner{ require(now > returnDate && rented); E_ReturnRental(Renter, now); resetRental(); } function resetRental() private{ rented = false; Renter.addr = address(0); rentalDate = 0; returnDate = 0; } }
我有一個名為 Car 的第二個契約,它與另一個契約互動
contract Car is rentCar { address public VechileOwner; string public make; string public model; Renter public renter; bool public available; uint public pricePerDay; uint public deposit; uint public entrycode; uint public minRentalDay; uint public maxRentalDay; function checkAvailability() public view returns (bool) { return(Car.available); } function Car(string _make, string _model, uint _pricePerDay, uint _minRentalDay, uint _maxRentalDay, bool _available) public onlyOwner{ make = _make; model = _model; pricePerDay = _pricePerDay; minRentalDay = _minRentalDay; maxRentalDay = _maxRentalDay; available = _available; } }
我真的很感激幫助。
非常感謝
您有幾個錯誤,最臭名昭著的就是將其聲明
Car
為契約。TypeError: Expression has to be an lvalue. newCar.VechileOwner = msg.sender; ^-----------------^
這
newCar
是Car
一個合約的實例,您不能直接訪問另一個合約的變數。Car newCar = cars[numofCars]; newCar.VechileOwner = msg.sender;
Solidity 將為公共變數創建 getter,但它不會創建你必須顯式創建它們的 setter。
TypeError: Type address is not implicitly convertible to expected type function () view external returns (address). newCar.VechileOwner = msg.sender; ^--------^
這裡的問題是
VechileOwner
getter(一個函式),你試圖給他們分配一個地址。這是不可能的。TypeError: Operator < not compatible with types uint256 and function () view external returns (uint256) require (msg.value < cars[objID].deposit); ^-----------------------------^
這裡的一個類似問題
cars[objID].deposit
是 getter,您將它與 an 進行比較uint
,它沒有任何意義。恕我直言,更好的解決方案是在契約中聲明
Car
一個結構。rentCar
struct Car { address VechileOwner; string make; string model; Renter renter; bool available; uint pricePerDay; uint deposit; uint entrycode; uint minRentalDay; uint maxRentalDay; }
這會導致目前程式碼出現其他問題。