"返回参数类型字符串存储 ref 不能隐式转换为预期类型(第一个返回变量的类型)字符串调用数据"的解决方案?



我是一个新手,我的第一个合同没有编码经验。有人能帮忙解决这个错误吗?错误消息是:"返回参数类型字符串存储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;
}**
}
function getGreetings() public view returns (string calldata)

calldata是在函数输入中初始化的只读数据位置。您可以在不同的场景中返回string calldata—如果您接受string calldata作为输入,然后返回相同的值。

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

由于您正在返回存储属性的值,其值从storage加载到memory(而不是calldata)。所以你需要返回string memory

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