我继续获得Parseerror:预期类型名称//我想返回刚创建的结构时

发布于 2025-01-23 02:30:46 字数 805 浏览 0 评论 0原文

我的坚固性是新手,我一直在尝试创建和获得结构而不将其添加到数组中。我一直在看到带有数组的结构和方法.push和我想尝试一下。

我已经与一个结构和一个功能创建了一个合同来创建和获取它。

如果我创建一个单个公共函数来创建但不返回,则它不会给我任何错误...如下:

    struct Todo {
    string name;
    uint age;
}

function createTodo(string memory _name, uint _age) public pure{
    Todo memory myTodo = Todo(_name, _age);

}

使用上述代码,我也想知道为什么它不会让我设置指针“ todo”作为存储,例如: todo storage mytodo = todo(_name,_age); 它给出了一个类型:todo内存并不是预期的结构存储指针的含义可转换。

接下来,我试图修改函数以创建和返回,但是当我开始获得ParseError时。

该代码如下:

 function getTodo(string memory _name, uint _age) public returns(struct myTodo) {
    Todo memory myTodo = Todo(_name, _age);
    return myTodo;

}

使用AOBVE代码,我还尝试了“返回(struct),返回(struct Memory)”。...

我真的很感谢这里的任何类型的帮助。

非常感谢

I am new in Solidity and I have been trying to create and get STRUCT without adding it to an Array. I alway see Structs with arrays and the method .push and I wanted to try it without it.

I have created a single Contract with one Struct and one function to create and get it.

If I create a single public function to create, but not return, the struct it doesn´t give me any error... Like the following:

    struct Todo {
    string name;
    uint age;
}

function createTodo(string memory _name, uint _age) public pure{
    Todo memory myTodo = Todo(_name, _age);

}

With the above code I would also like to know why it wouldn´t let me set the pointer "Todo" as storage like: Todo storage myTodo = Todo(_name, _age); It gives an TypeError: Todo memory is not implicity convertible to expect type struct storage pointer.

Next, I tried to modify the function to create and RETURN but then is when I start getting the ParseError.

The code is the following:

 function getTodo(string memory _name, uint _age) public returns(struct myTodo) {
    Todo memory myTodo = Todo(_name, _age);
    return myTodo;

}

With the aobve code I also tried in "returns(struct), returns(struct memory)"....

I would really appreciate any type of help here.

Thank you very much

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

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

发布评论

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

评论(1

寂寞花火° 2025-01-30 02:30:47

您收到此错误,因为您错误地在getTodo()函数上设置返回关键字。在您的情况下,您必须以这种方式更改功能:

function getTodo(string memory _name, uint _age) external pure returns(Todo memory) {
        Todo memory myTodo = Todo(_name, _age);
        return myTodo;
}

如果要处理存储结构,请参阅此智能合约代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Test {
    
    struct Todo {
        string name;
        uint age;
    }

    // Declare state variable
    Todo[] todoArray;

    // Push into array new struct
    function createTodo(string memory _name, uint _age) public {
        todoArray.push(Todo(_name, _age));
    }

    // Retrieve ToDo struct from specific index about struct array
    function getTodo(uint _index) external view returns(Todo memory) {
        return todoArray[_index];
    }
} 

You received this error because you're wrong to set a returns keyword on getTodo() function. In your case, you must change your function in this way:

function getTodo(string memory _name, uint _age) external pure returns(Todo memory) {
        Todo memory myTodo = Todo(_name, _age);
        return myTodo;
}

If you want to handle storage structs, see this smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Test {
    
    struct Todo {
        string name;
        uint age;
    }

    // Declare state variable
    Todo[] todoArray;

    // Push into array new struct
    function createTodo(string memory _name, uint _age) public {
        todoArray.push(Todo(_name, _age));
    }

    // Retrieve ToDo struct from specific index about struct array
    function getTodo(uint _index) external view returns(Todo memory) {
        return todoArray[_index];
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文