对于函数中的 return 参数,数据位置必须是 "memory",但给出了 none

Data location must be "memory" for return parameter in function, but none was given

我在 remix 中尝试了上面的 solidity 示例,solidity 版本 > 0.5.0 但我现在收到这个错误。 有什么方法可以解决这个错误?

contract MyContract {
    string value;

    function get() public view returns (string) {
        return value;
    }

    function set(string _value) public {
        value = _value;
    }

    constructor() public {
        value = "myValue";
    }
}

你应该为字符串参数添加 memory 关键字,这是在 solidity 版本 0.5.0 中引入的

根据文档:

Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[][] memory x) where memory is the data location and might be replaced by storage or calldata accordingly. Note that external functions require parameters with a data location of calldata.

更正代码

contract MyContract {
    string value;

    function get() public view returns (string memory) {
        return value;
    }

    function set(string memory _value) public {
        value = _value;
    }

    constructor() public {
        value = "myValue";
    }
}

参考官方documentation 0.5.0版本的重大改动

Solidity 每天都会更新,因此您应该注意其中的一些变化。为此,请继续参考更新的 solidity 文档。

代码应该是这样的:

contract MyContract {
    string value;

    function get() public view returns (string memory) {
        return value;
    }

    function set(string memory _value) public {
        value = _value;
    }

    constructor() public {
        value = "myValue";
    }
}

contract MyContract {
    string value;

    function get() public view returns (string calldata) {
        return value;
    }

    function set(string calldata _value) public {
        value = _value;
    }

    constructor() public {
        value = "myValue";
    }
}

Values of reference type can be modified through multiple different names. Contrast this with value types where you get an independent copy whenever a variable of value type is used. Because of that, reference types have to be handled more carefully than value types. Currently, reference types comprise structs, arrays and mappings. If you use a reference type, you always have to explicitly provide the data area where the type is stored: memory (whose lifetime is limited to an external function call), storage (the location where the state variables are stored, where the lifetime is limited to the lifetime of a contract) or calldata (special data location that contains the function arguments).

Warning

Prior to version 0.5.0 the data location could be omitted, and would default to different locations depending on the kind of variable, function type, etc., but all complex types must now give an explicit data location.

https://docs.soliditylang.org/en/latest/types.html#reference-types

所以你必须在 String 后面加上 memorycalldata 如下:

contract MyContract {
    string value;

    function get() public view returns (string memory) {
        return value;
    }

    function set(string memory _value) public {
        value = _value;
    }

    constructor() {
        value = "myValue";
    }
}

另一件需要注意的事情是您不必再将 public 放入构造函数中:

Warning: Prior to version 0.7.0, you had to specify the visibility of constructors as either internal or public.

https://docs.soliditylang.org/en/latest/contracts.html?highlight=constructor#constructors

如果是 return 地址数组,您可以在 return address type.

之后声明 memory
function getAllPlayers() public view returns(address[] memory){
        return players;
    }

pragma solidity 0.8.11;

contract Greeter{

    string greeting;

    function greeter(string memory _greeting) public{
        greeting = _greeting;

    }

    function greet() public returns(string memory)
    {
        return greeting;
    }

}