成员 "push" 在参数相关查找后未找到或不可见
Member "push" not found or not visible after argument-dependent lookup
问题
此行似乎有一些问题 family.People.push(people[x]);
当我尝试使用 brownie 进行编译时,我一直收到 Member "push" not found or not visible after argument-dependent lookup
。
我尝试了什么
我看到一些有类似异常的 SO 帖子,但它与类型转换有关。我确实尝试将我的传入数组转换为它的类型,但它只会导致更多异常。
代码
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++){
family.People.push(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}
任何人都可以发现我在这里做错了什么或 link 一篇可以找到答案的文章吗?
我认为这与access modifiers
有关。对数组使用 public
修饰符只会为其生成 getter
函数,而不是 setter
。
因此,您不能直接从另一个合约推送到数组。我创建了一个 public
函数来向数组添加元素,如下所示:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
function addPerson(Person person) public {
People.push(person);
}
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++) {
family.addPerson(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}
问题
此行似乎有一些问题 family.People.push(people[x]);
当我尝试使用 brownie 进行编译时,我一直收到 Member "push" not found or not visible after argument-dependent lookup
。
我尝试了什么
我看到一些有类似异常的 SO 帖子,但它与类型转换有关。我确实尝试将我的传入数组转换为它的类型,但它只会导致更多异常。
代码
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++){
family.People.push(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}
任何人都可以发现我在这里做错了什么或 link 一篇可以找到答案的文章吗?
我认为这与access modifiers
有关。对数组使用 public
修饰符只会为其生成 getter
函数,而不是 setter
。
因此,您不能直接从另一个合约推送到数组。我创建了一个 public
函数来向数组添加元素,如下所示:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Person{
string public FirstName;
string public LastName;
}
contract Family{
Person[] public People;
function addPerson(Person person) public {
People.push(person);
}
}
contract FamilyManager{
Family[] Families;
function AddFamily(Person[] memory people) public {
Family family = new Family();
for(uint x; x < people.length; x++) {
family.addPerson(people[x]);
}
Families.push(family);
}
function GetFamilies() public view returns (Family[] memory){
return Families;
}
}