[英]Error invalid opcode trying to access Solidity array of structs in Hardhat
I have the following struct and array in my Solidity code:我的 Solidity 代码中有以下结构和数组:
struct Character {
int256 strength;
uint256 dexterity;
uint256 constitution;
....
}
Character[] public characters;
I have the following line in my Hardhat test attempting to access a member of that array:我在尝试访问该数组成员的安全帽测试中有以下行:
const character = await contract.characters(0)
I then get the following error:然后我收到以下错误:
Error: VM Exception while processing transaction: invalid opcode
at Contract.characters
What is the correct way to access a member of this array of structs?访问此结构数组成员的正确方法是什么?
As pointed out in the question comment, the characters
array was empty when the characters(0)
function was called.正如问题评论中所指出的,调用characters(0)
函数时, characters
数组为空。
Solidity autogenerates getter functions for public
arrays, allowing to access an item of the array using its index. Solidity 为public
数组自动生成 getter 函数,允许使用其索引访问数组的项目。
When you try to access an index that is out of bounds, the EVM throws an exception.当您尝试访问越界的索引时,EVM 会引发异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.