成员“推”在参数依赖性查找之后找不到或看不到

发布于 2025-01-23 19:03:30 字数 912 浏览 0 评论 0原文

问题
此行似乎存在一些问题family.people.push(people [x]);

我一直在ement ement'push'''supl''disper'找不到或在参数依赖性依赖性查找之后< /代码>当我尝试与布朗尼编译时。

我尝试了什么
我看到了一些帖子,但与类似的帖子有关。我确实试图将传入的数组投射到其类型上,但这只是导致了更多例外。

代码

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;
    }
}

任何人都可以在这里发现我在做错什么还是链接到可以带来答案的文章?

Issue
There seems to be some issue with this line here family.People.push(people[x]);

I keep getting Member "push" not found or not visible after argument-dependent lookup when I try to compile with brownie.

What have I tried
I saw a few SO posts with similar exceptions but it was related to type casting. I did try to cast my incoming array to its type but it just resulted in more exceptions.

Code

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;
    }
}

Can anyone spot what I'm doing wrong here or link to an article that can lead to an answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2025-01-30 19:03:30

我认为这与访问修饰符有关。使用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;
    }
}

I think it is related to access modifiers. Using the public modifier for your array only generates the getter function for it and not the setter.
As a result, you cannot directly push to an array from another contract. I created a public function to add elements to the array as follows:

// 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;
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文