“返回参数类型字符串存储ref的解决方案”不可隐式转换为预期类型(first返回变量的类型)字符串calldata&quort'?
我是一个新的合同,没有编码经验。有人可以帮助解决此错误吗?错误消息是:“返回参数类型字符串存储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;
}**
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
calldata
是一个不读取在功能输入中初始化。您可以在不同的方案中返回字符串calldata
- 如果您接受字符串calldata
作为输入,然后返回相同的值。由于您要返回存储属性的值,因此其值从
存储
内存(不是calldata
)。因此,您需要返回字符串内存
。calldata
is a read-only data location initialized in the function input. You could returnstring calldata
in a different scenario - if you accepted astring calldata
as an input, and then returned the same value.Since you're returning the value of a storage property, its value is loaded from
storage
tomemory
(not tocalldata
). So you need to returnstring memory
.