You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Solidity嵌套struct编译提示需启用ABIEncoderV2错误如何解决?

错误原因

你当前使用的Solidity版本默认ABI编码器不支持将自定义结构体作为公开函数的返回值,你代码中getPerson和getHistoryRecord两个函数的返回值都包含了自定义Person结构体,因此触发了编译报错。结构体嵌套本身是合法操作,并非问题根源。

解决方案

提供两种可选方案,你可以根据自己的需求选择:

方案1:开启ABI编码器V2(改动最小)

只需要在合约文件顶部添加一行配置即可解决,无需修改现有业务逻辑:

  • 如果你使用的Solidity版本 < 0.8.0:在pragma solidity xxx;行下方添加pragma experimental ABIEncoderV2;
  • 如果你使用的Solidity版本 >= 0.8.0:在pragma solidity xxx;行下方添加pragma abicoder v2;(0.8版本后该特性已稳定,无需标注experimental)

方案2:拆解返回值(兼容性最好,无需启用实验特性)

如果你不想修改编译器配置,可以将返回结构体的逻辑改为返回结构体的每个独立字段,对应修改两个函数即可:

  1. 修改getPerson函数:
function getPerson() public view returns (uint ppNo, bytes32 firstName, string memory gender, string memory dayOfbirth, string memory country) {
    Person memory person = PersoneDictionary[msg.sender];
    return (person.ppNo, person.firstName, person.gender, person.dayOfbirth, person.country);
}
  1. 修改getHistoryRecord函数:
function getHistoryRecord(uint index) public view returns 
(uint256 incidentTime, address owner, uint ppNo, bytes32 firstName, string memory gender, string memory dayOfbirth, string memory country) {
    checkAdminPermission();
    HistoryRecord memory record = historyRecords[index];
    return (record.incidentTime, record.owner, record.person.ppNo, record.person.firstName, record.person.gender, record.person.dayOfbirth, record.person.country);
}
额外提示

你的代码里存在两处拼写笔误,不影响编译但建议修正:

  • 合约名Compeny应为Company
  • 映射名PersoneDictionary应为PersonDictionary

内容的提问来源于stack exchange,提问作者Fatima Almashhor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 16:45:04