0

Usually this error shows up when declaring functions and goes away by adding "memory" next to the argument, but this time I can't get it to resolve. I'm trying to instantiate a struct that takes in two parameters:

struct Obj {
    string where;
    uint what;
    }


function do() public {
    obj=Obj("here",1);
    }

the line with the new Obj triggers: TypeError: Data location must be "storage", "memory" or "calldata" for variable, but none was given.

I tried putting "memory" all over the place, after the first argument, after the second, after both, after the function, etc. Nothing seems to appease the compiler.


I'm using pragma solidity ^0.8.0;

1 Answer 1

1

This function works :

function foo() public {
Obj memory obj=Obj("here",1);
}

Solidity is a statically typed language so you have to specify the type of each variable.

For the difference between storage, memory, and calldata, you can check these two links :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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