0

I wanted to do a get() to return some contract information, including an array, but when I put it gives this error:

browser/leilaoSimples.sol:34:9: TypeError: Data location must be "memory" for return parameter in function, but none was given. uint[] _historicoDeOfertas ^------------------------^

function get() public view returns(
        address _dono,
        uint _contadorDeOfertas,
        address _comprador,
        uint _ultimaOferta,
        uint[] _historicoDeOfertas
    ){
        return(
            dono,
            contadorDeOfertas,
            comprador,
            valorAtual,
            ofertas
        );
    }

Full code (Note: it's in Portuguese)

pragma solidity >=0.5.1 <0.6.0;

contract leilaoSimples{
    uint valorAtual;
    address payable comprador;
    address dono;
    string nome;
    uint contadorDeOfertas = 0;
    uint[] ofertas;

    constructor(uint valorInicial) public{
        valorAtual=valorInicial;
        dono = msg.sender;
    }

    function set() public payable{
        uint oferta = msg.value;
        require(oferta>valorAtual, "Oferta abaixo do valor atual.");
        require(comprador!=msg.sender, "Você deu o último lance.");

        comprador.transfer(valorAtual);

        valorAtual = oferta;
        contadorDeOfertas++;
        ofertas.push(valorAtual);
        comprador = msg.sender;
    }

    function get() public view returns(
        address _dono,
        uint _contadorDeOfertas,
        address _comprador,
        uint _ultimaOferta,
        uint[] _historicoDeOfertas
    ){
        return(
            dono,
            contadorDeOfertas,
            comprador,
            valorAtual,
            ofertas
        );
    }
}
1

As the error suggests, you have to add a memory keyword for your _historicoDeOfertas return value. So the function signature becomes:

function get() public view returns(
        address _dono,
        uint _contadorDeOfertas,
        address _comprador,
        uint _ultimaOferta,
        uint[] memory _historicoDeOfertas
    ){
        return(
            dono,
            contadorDeOfertas,
            comprador,
            valorAtual,
            ofertas
        );
    }

You can find some explanation about the memory here: What does the keyword "memory" do exactly? and especially in its link here: https://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html#storage-memory-and-the-stack

  • To be fair, I spent some 20 minutes trying to read different sources about why exactly the memory is required there but I didn't understand it - why couldn't it return a storage pointer? I hope someone else can post a better answer which explains this. – Lauri Peltonen Sep 18 at 18:10
  • 1
    Possibly not as precise as you would like...storage (pointers) are acceptable inputs/return values within contracts, so acceptable for functions with internal or private visibility. This is a public function so it can't simply pass a pointer to its internal storage to an external caller. – Rob Hitchens - B9lab Sep 19 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.