0

I'm using the remix IDE.I want to define the size of the array at the time constructor is called. Someone please help. This is what I tried :

pragma solidity >=0.7.0 <0.8.0;
contract Vote
{   
    struct Proposal
    {
        uint votecount;
    }
    Proposal[] proposal;   
            
    constructor(uint no)
    {           
        proposal=new Proposal[](no);     
    }    
}

Error: UnimplementedFeatureError: Copying of type struct Vote.Proposal memory[] memory to storage not yet supported. Another :

pragma solidity >=0.7.0 <0.8.0;
contract Vote
{               
    struct Proposal
    {
        uint votecount;
    }
    Proposal[] proposal;
                        
    constructor(uint no)
    {               
        for(uint i=0;i<no;i++)
            proposal.votecount.push(0);
    }    
}

Error: TypeError: Member "votecount" not found or not visible after argument-dependent lookup in struct Vote.Proposal storage ref[] storage ref. proposal.votecount.push(0); ^----------------^

1

The Proposal[] proposal declaration in your code is of a storage array.

As such, you don't need to initialize it with proposal = new Proposal[](...);.

You can simply push into it as many items as you'd like.

The only thing actually missing there is an access-level (public/internal/private).


As with regards to your push attempt, simply change this:

proposal.votecount.push(0);

To this:

proposal.push(Proposal(0));

Or better yet, to this:

proposal.push(Proposal({votecount: 0}));

UPDATE:

There are generally 4 types of arrays that you should know about:

  1. A static local array - declared with a constant size, inside a function
  2. A dynamic local array - declared without a constant size, inside a function
  3. A static global array - declared with a constant size, outside of all functions
  4. A dynamic global array - declared without a constant size, outside of all functions

Types 1 and 2 reside in memory.

Types 3 and 4 reside in storage.

Type 2 is the only one which you need to allocate before attempting to use, for example:

function func(uint len) {
    uint[] memory arr = new uint[](len);
    ...
}
3
  • What do you mean by storage array? Will my code work if the array was locally defined? Why can't I simply define the size in a constructor like I would have in another programming language like Java? I'm new to solidity programming, still learning the basics.
    – Vasvi Sood
    Jan 4 at 16:37
  • @VasviSood: See updated answer. Jan 4 at 16:53
  • Thanks, I understand the concept now.
    – Vasvi Sood
    Jan 5 at 6:12
1

Your array is composed of Proposal but you are trying to push a 0 into a struct member.

You can only push a Proposal on to an array of Proposal. Try this:

pragma solidity 0.7.6;

// SPDX-License-Identifier: UNLICENSED

contract Vote {
           
    struct Proposal {
        uint votecount;
    }
    
    Proposal[] proposals;
        
    constructor(uint no) {
        Proposal memory p;
        for(uint i=0; i<no; i++) {
            proposals.push(p);
        }
    }    
}

Hope it helps.

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.