将结构体保存在其他结构体内部



我正在开发一个小型以太坊网站项目。我知道我是新手。我开始学习坚实的基础,但我有一个问题,我不知道如何解决。我希望有人能帮我。

在这里的代码中,我想使用Person数据作为结构体到HistoryRecode,以便跟踪他/她的信息。但是我得到了这个错误。
project:/contracts/Passport.sol:102:47: TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.
function getPassport() public view returns (Person memory) {
^-----------^
,project:/contracts/Passport.sol:108:41: TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.
(uint256 incidentTime, address owner, Person memory person) {
^------------------^
Compilation failed. See above.
Truffle v5.4.3 (core: 5.4.3)
Node v16.3.0
contract Compeny{
address constant  ADMIN_ADDRESS = 0x8c0199C5D6e4B22A1948358F1bf48dD095Ae5300; 
struct Person {
uint ppNo ;
bytes32  firstName;
string gender ;
string dayOfbirth ;
string country ;
}
mapping(address => Person) private PersoneDictionary;
HistoryRecord[] private historyRecords;
function createOrUpdatePassport(
address _owner,
uint _ppNo ,
bytes32 _firstName ,
string memory _gender ,
string memory _dayOfbirth ,
string memory _country 
) public 
{     
checkAdminPermission();
Person memory person = Person(
_ppNo ,
_firstName ,
_gender ,
_dayOfbirth, 
_country 
) ;
PersoneDictionary[_owner] = person;
historyRecords.push(HistoryRecord({
incidentTime : block.timestamp ,
owner : _owner,
person : person 
}));
}
function getPerson() public view returns (Person memory) {
return PersoneDictionary[msg.sender];
}

function getHistoryRecord(uint index) public view returns 
(uint256 incidentTime, address owner, Person memory person) {
checkAdminPermission();
return (historyRecords[index].incidentTime, 
historyRecords[index].owner, historyRecords[index].person);
}
struct HistoryRecord {
uint256 incidentTime;
address owner;
Person  person;
}
// utility functions
function getHistoryRecordLength() public view returns (uint) {
checkAdminPermission();
return historyRecords.length;
}
function checkAdminPermission() private view {
if (msg.sender != ADMIN_ADDRESS) {
revert();
}
}
}

据我所知,除非您将pragma experimental ABIEncoderV2添加到您的合同中,否则您无法返回struct。顾名思义,它是实验性的,也仅限于internal函数。

看一下这里的问题

那么你能做的就是像这样单独返回所有的struct值:

function getPerson() 
public view returns(uint ppNo,  bytes32 firstName, string memory gender, string memory dayOfbirth, string memory country) 
{
return (PersoneDictionary[msg.sender].ppNo, PersoneDictionary[msg.sender].firstName, 
PersoneDictionary[msg.sender].gender, PersoneDictionary[msg.sender].dayOfbirth, 
PersoneDictionary[msg.sender].country);
}

相关内容

  • 没有找到相关文章

最新更新