稳定性:在结构体中向嵌套数组中添加项



我正在尝试添加一个项目到一个数组,这是一个结构体,内部的结构体。

我的合同是这样的:

contract AuctionHouse {
struct Bid {
address bidder;
uint256 amount;
uint256 timestamp;
}

struct Auction {
address seller;
uint256 reservePrice;
uint256 winnerBidAmount;
address winnerBidBidder;

Bid[] Bids;
}

function listItem (uint256 _tokenId) public returns (bool) {
Bid[] memory emptyBids;
auctions[_tokenId] = Auction({
seller: msg.sender,
reservePrice: _reservePrice,
winningBidAmount: 0,
winningBidBidder: address(0),
bids: emptyBids
});
return true;
}

function bid (uint256 _tokenId, uint256 _amount) public returns (bool) {
Auction currentAuction = auctions[_tokenId];

uint256 newBidIndex = currentAuction.bids.length + 1;
Bid newBid = currentAuction[newBidIndex];
newBid.bidder = msg.sender;
newBid.amount = msg.amount;
newBid.timestamp = block.timestamp;
currentAuction.winningBidAmount = msg.value;
currentAuction.winningBidBidder = msg.sender;

return true;
}
}

但是我得到错误:

contracts/AuctinoHouse.sol:274:26: TypeError: Indexed expression has to be a type, mapping or array (is struct TripAuctionHouse.Auction storage pointer)
Bid storage newBid = currentAuction[newBidIndex];

我也试着在竞标函数里面改变:

Auction currentAuction = auctions[_tokenId];

storagememory,并试图玩弄它,但会产生不同的错误。

正确的做法是什么?

您以错误的方式捕获newBid

Bid newBid = currentAuction[newBidIndex];

这将返回Auction结构,而不是Bid

我像这样替换了代码:

function bid (uint256 _tokenId, uint256 _amount) public returns (bool) {
Auction currentAuction = auctions[_tokenId];

uint256 newBidIndex = currentAuction.bids.length + 1;
Bid newBid = currentAuction.Bids[newBidIndex];
newBid.bidder = msg.sender;
newBid.amount = msg.amount;
newBid.timestamp = block.timestamp;
currentAuction.winningBidAmount = msg.value;
currentAuction.winningBidBidder = msg.sender;

return true;
}

相关内容

  • 没有找到相关文章

最新更新