“返回参数类型字符串存储ref的解决方案”不可隐式转换为预期类型(first返回变量的类型)字符串calldata&quort'?

发布于 2025-02-12 19:17:54 字数 1700 浏览 0 评论 0 原文

我是一个新的合同,没有编码经验。有人可以帮助解决此错误吗?错误消息是:“返回参数类型字符串存储REF并非隐式转换为预期类型(类型的第一返回变量)字符串CallData。”该错误是对GetGreetings函数的响应,即ln 27,col 16,其中“返回消息”;是。

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.0;

contract GreetingsContract {
    /** This is a state variable of type string called message. This is an instance variable used to store the greetings message.
    The string data type means that the variable will contain a variable length array of characters. */
    string message;

/** This is a constructor "Greetings" with no paramater and no return value. This is public so that it can be called outside of the contract. 
Solidity lets us define a constructor that will be called only once when the contract is first deployed to the blockchain. 
The constructor does not return any value in the contract. */
    function Greetings() public {
        message =  "I'm ready!";
    }

/** This will take one parameter of type string and the name will be "_message" so we can differentiate with the internal state variable.
We'll only alter the state of the contract to overwrite the internal message with the argument. 
This function will alter the instance variable with the value sent in parameter. This is also public and doesnt return anything. 
This is often the case for functions that modify the state of the contract because there is currently no way to acces the values returned by such a function. */
    function setGreetings(string calldata _message) public {
        message = _message;
    }

/**View will return the string and the message. */
    **function getGreetings() public view returns (string calldata) {
        return message;
    }**
}

I'm a newb doing my first contract with no coding experience. Can someone help with resolving this error please? The error message is: "Return argument type string storage ref is not implicitly convertible to expected type (type of first return variable) string calldata." The error is in response to the getGreetings function, which is Ln 27, Col 16 where "return message;" is.

// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity ^0.8.0;

contract GreetingsContract {
    /** This is a state variable of type string called message. This is an instance variable used to store the greetings message.
    The string data type means that the variable will contain a variable length array of characters. */
    string message;

/** This is a constructor "Greetings" with no paramater and no return value. This is public so that it can be called outside of the contract. 
Solidity lets us define a constructor that will be called only once when the contract is first deployed to the blockchain. 
The constructor does not return any value in the contract. */
    function Greetings() public {
        message =  "I'm ready!";
    }

/** This will take one parameter of type string and the name will be "_message" so we can differentiate with the internal state variable.
We'll only alter the state of the contract to overwrite the internal message with the argument. 
This function will alter the instance variable with the value sent in parameter. This is also public and doesnt return anything. 
This is often the case for functions that modify the state of the contract because there is currently no way to acces the values returned by such a function. */
    function setGreetings(string calldata _message) public {
        message = _message;
    }

/**View will return the string and the message. */
    **function getGreetings() public view returns (string calldata) {
        return message;
    }**
}

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

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

发布评论

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

评论(1

∞觅青森が 2025-02-19 19:17:54
function getGreetings() public view returns (string calldata)

calldata 是一个不读取在功能输入中初始化。您可以在不同的方案中返回字符串calldata - 如果您接受字符串calldata 作为输入,然后返回相同的值。

function foo(string calldata inputString) public pure returns (string calldata) {
    return inputString;
}

由于您要返回存储属性的值,因此其值从存储 内存(不是 calldata )。因此,您需要返回字符串内存

function getGreetings() public view returns (string memory) {
    return message;
}
function getGreetings() public view returns (string calldata)

calldata is a read-only data location initialized in the function input. You could return string calldata in a different scenario - if you accepted a string calldata as an input, and then returned the same value.

function foo(string calldata inputString) public pure returns (string calldata) {
    return inputString;
}

Since you're returning the value of a storage property, its value is loaded from storage to memory (not to calldata). So you need to return string memory.

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