Solidity ParserError: Expected identifier but got '='

14,284

The grammar doesn't allow assignments on contract level. But it allows declarations of state variables and these can contain an initializer. Therefore you can initialize it with

Box public box = Box({ size: 3 });

or

Box public box = Box(3);
Share:
14,284
sunwarr10r
Author by

sunwarr10r

Updated on June 27, 2022

Comments

  • sunwarr10r
    sunwarr10r almost 2 years

    Why does the code below contain an error (ParserError: Expected identifier but got '=').

    contract Test {
    
        struct Box {
            uint size;
        }
    
        Box public box;
        box.size = 3;    //<-- error here
    
        constructor() public {
        }
    
    }
    

    It works if I put the box.size = 3; into the constructor!

    contract Test {
    
        struct Box {
            uint size;
        }
    
        Box public box;
    
        constructor() public {
            box.size = 3;
        }
    
    }