0

Why does this not work

struct Structure {
    string _var;
}

Structure[] structure;

function addStructure(string _var) {
    Structure _s = Structure(_var);
    ->> structure.push(_s);
}

while this works

struct Structure {
    string _var;
}

Structure[] structure;

function addStructure(string _var) {
    X
    ->> structure.push(Structure(_var));
}

I saw this in an tutorial but it does not work for me. Thank you for explanation!

  • What error are you getting? – Andrey Feb 28 '18 at 1:28
2

Try using the memory keyword:

struct Structure {
    string _var;
} 

Structure[] structure;

function addStructure(string _var) {
    Structure memory _s = Structure(_var);
    structure.push(_s);
}

The way you wrote it, _s was interpreted as a reference to storage (permanent) memory. The compiler will not allow an implicit conversion of the temporary memory variable into a storage reference. The push copies the memory ref in to the permanent structure array.

  • Thank you very much, that solved it and gave me a tip on where to search! – dkb Feb 28 '18 at 12:31

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.