恐慌:索引越界 |坚固性 |松露
Panic: Index out of bounds | Solidity | truffle
我正在尝试使用松露框架在 ganache-cli 中部署以下智能合约。
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Sample {
address[] public owners;
constructor(address[] memory temp) {
for(uint256 i=0; i<temp.length; i++) {
owners[i] = temp[i];
}
}
}
但是我收到以下错误。
Deploying 'Sample'
------------------
✖ Transaction submission failed with error -32000: 'VM Exception while processing transaction: revert'
*** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.
Error: *** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
帮我解决这个问题。
address[] public owners;
此行声明了包含 0 个项目的数组。
owners[i] = temp[i];
并且此行试图分配给数组中不存在的第 i
个项目。
解决方法:需要使用push()
函数调整数组大小并设置新的项值。
for(uint256 i=0; i<temp.length; i++) {
owners.push(temp[i]);
}
我正在尝试使用松露框架在 ganache-cli 中部署以下智能合约。
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Sample {
address[] public owners;
constructor(address[] memory temp) {
for(uint256 i=0; i<temp.length; i++) {
owners[i] = temp[i];
}
}
}
但是我收到以下错误。
Deploying 'Sample'
------------------
✖ Transaction submission failed with error -32000: 'VM Exception while processing transaction: revert'
*** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.
Error: *** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
帮我解决这个问题。
address[] public owners;
此行声明了包含 0 个项目的数组。
owners[i] = temp[i];
并且此行试图分配给数组中不存在的第 i
个项目。
解决方法:需要使用push()
函数调整数组大小并设置新的项值。
for(uint256 i=0; i<temp.length; i++) {
owners.push(temp[i]);
}