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,370 points
9,478 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
• 69,220 points

Related Questions In Blockchain

0 votes
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,400 points
5,052 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 25, 2018 in Blockchain by Shashank
• 10,400 points
9,138 views
0 votes
1 answer

Web3j v3.3.1 : Error while generating compiled solidity smart contracts which returns array of struct

Solidity does not support returning structs in ...READ MORE

answered Jun 19, 2018 in Blockchain by aryya
• 7,460 points
1,670 views
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
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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
• 69,220 points
1,464 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
• 69,220 points
2,496 views
0 votes
3 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP