Solidity ParserError: Expected identifier but got '='

0 votes

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;
    }

}
Oct 9, 2018 in Blockchain by slayer
• 29,050 points
253 views

1 answer to this question.

0 votes

Hey, the solidity 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);
answered Oct 9, 2018 by Omkar
• 67,120 points

Related Questions In Blockchain

+1 vote
1 answer

What is the difference between if() and require() statement in solidity??

If() and require() have separate functions and ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,330 points
300 views
+1 vote
1 answer

what is use of msg.sender in solidity code?

msg.sender(address) function indicated the sender of the ...READ MORE

answered Apr 24, 2018 in Blockchain by Shashank
• 10,330 points
308 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
105 views
0 votes
1 answer
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 67,120 points
54 views
0 votes
1 answer

Truffle Compile failing, ParserError: Expected token LParen got Identifier function

Your parentheses are mismatched. pragma solidity ^0.4.17; import "truffle/Assert.sol"; import ...READ MORE

answered Oct 9, 2018 in Blockchain by Omkar
• 67,120 points
176 views