你能告诉我为什么我会在坚固性上获取此错误消息

发布于 2025-01-21 08:46:01 字数 1599 浏览 0 评论 0 原文

从坚固性:

DeclarationError: Identifier already declared.
  --> contracts/MySimpleStorage.sol:16:5:
   |
16 |     people[] public people;
   |   
Note: The previous declaration is here:
  --> contracts/MySimpleStorage.sol:11:5:
   |
11 |     struct people {
   |     (Relevant source part starts here and spans across multiple lines).

错误2

来自坚固性:

TypeError: Expected callable expression before call options.
   contracts/MySimpleStorage.sol:32:21:
   |
32 |         people.push(people{favoriteNumber: _favoriteNumber, name: _name});

crefenenshot用于参考

这是主要代码

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract MySimpleStorage {

    //this will get initilized to 0 since we did not state the number
    uint256 public favoriteNumber;
    bool favoriteBool;

    struct people{
        uint256 favoriteNumber;
        string name;
    }

    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;

    }

    
    function retrieve() public view returns(uint256) {
        return favoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public{
        people.push(people(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }

}

from solidity:

DeclarationError: Identifier already declared.
  --> contracts/MySimpleStorage.sol:16:5:
   |
16 |     people[] public people;
   |   
Note: The previous declaration is here:
  --> contracts/MySimpleStorage.sol:11:5:
   |
11 |     struct people {
   |     (Relevant source part starts here and spans across multiple lines).

error 2

from solidity:

TypeError: Expected callable expression before call options.
   contracts/MySimpleStorage.sol:32:21:
   |
32 |         people.push(people{favoriteNumber: _favoriteNumber, name: _name});

screenshot for reference

here's the main code

// SPDX-License-Identifier: MIT

pragma solidity >=0.7.0 <0.9.0;

contract MySimpleStorage {

    //this will get initilized to 0 since we did not state the number
    uint256 public favoriteNumber;
    bool favoriteBool;

    struct people{
        uint256 favoriteNumber;
        string name;
    }

    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;

    }

    
    function retrieve() public view returns(uint256) {
        return favoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public{
        people.push(people(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }

}

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

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

发布评论

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

评论(2

悲凉≈ 2025-01-28 08:46:02

这个问题来自案例敏感性。您已经在此处声明了结构较低的结构
您需要在此之前的动态阵列之前声明相同[]
小写是您的对象名称出现在公共按钮上,这可能是公开的人
bottonname.push(structname ...

This issue is from case sensitivity. you have declared struct with lower case here -struct people
You will need to declare same before the dynamic array here-People[]
Lowercase here is your object name that appears on the button in public, and this could be anything -public people
Bottonname.push(structNAME...

千里故人稀 2025-01-28 08:46:01

由于以下错误的错误:

  1. 从坚固性:SectarationError:标识符已经声明,因此您面临错误。
  • 您将struct声明为(小写 - 人) - struct people {
  • ,但将其用作(uppercase -people []) - people []公共人员;

上述 > People 已经在结构中声明,您再次将 People 称为数组名称。

相反,应该是:

struct People{ 
   People[] public people;

因为以坚固的方式,结构名称通常按照普通命名约定为大写。
因此,被声明为类型 People [] 的公共数组。

  1. 从坚固性:

    TypeError:通话选项之前的预期可呼叫表达式

,是由于以下语句中缺少括号:
people.push(people {fairiteNumber:_favoriteNumber,name:_name});

而不是:应该是:
people.push( people( {fairiteNumber:_favoriteNumber,name:_name} );

我希望这会有所帮助!

You are facing errors because of few the below-mentioned errors:

  1. from solidity: DeclarationError: Identifier already declared.
  • you declared the struct as (lowercase - people) - struct people{
  • but used it as ( uppercase - People[] ) - People[] public people;

The aforementioned people is already declared in a struct, and you are again declaring people as an array name.

Instead, it should be:

struct People{ 
   People[] public people;

because in Solidity, struct names are typically capitalized as per the common naming conventions.
So people is declared as a public array of type People[].

  1. from solidity:

    TypeError: Expected callable expression before call options

It is due to a missing bracket in the below statement:
people.push(people{favoriteNumber: _favoriteNumber, name: _name});

Instead, it should be:
people.push(people({favoriteNumber: _favoriteNumber, name: _name}));

I hope this helps!

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