0

I'm currently fighting with a seemingly very simple issue. I just try to increment the value stored in an array in a Solidity contract.

uint256[] public barLengthToBarAmtForSale;

I initialise the array in the constructor...

uint16 counter = uint16(0);
    while(counter <= 10) {
        barLengthToBarAmtForSale.push(uint256(0));
        counter++;
    }

Later I try to increment values in this array:

uint16 _length = 5;
barLengthToBarAmtForSale[_length]++;

But the smart contract doesn't compile bc of this line :(

What might be the reason?

Thank you.

Edit: sorry for the misleading name of _length parameter. It's not the length of the array. It's just an example value. I just checked my implementation using remix and there it compiles. However, when using truffle migrate, it doesn't work.

  • Can you post your actual code? We can't see where _length is supposed to be coming from. – Edmund Edgar Jul 27 '18 at 8:29
  • (Noting you've now updated the question... ) Is _length the length of the array? If it is, you'll want _length - 1 as the index as arrays are 0-indexed, meaning the first item is at index 0 and the final item is at _length - 1. Anything else is off the end. (Failing this, we'll need to see the rest of the code to see how _length is manipulated.) – Richard Horrocks Jul 27 '18 at 8:58
0

I put together your code, and it works.

contract Mycontract {

    uint256[] public barLengthToBarAmtForSale;

    constructor() public {
        uint16 counter = uint16(0);
        while(counter <= 10) {
            barLengthToBarAmtForSale.push(uint256(0));
            counter++;
        }
    }

    function test() public {
        uint16 _length = 5;
        barLengthToBarAmtForSale[_length]++;
    }


}

Note that this code is equivalent:

contract Mycontract {

    uint256[10] public barLengthToBarAmtForSale;

    function test() public {
        uint16 _length = 5;
        barLengthToBarAmtForSale[_length]++;
    }


}
0

I did not understand why your does not compile. Everything seems okay, but any missing semicolon or curly bracket can arise unrelated errors like you have. Below is the similar code which I derived from your code, that works for me. My environment :

  • Solidity ^0.4.24
  • Compiler Current version:0.4.24+commit.e67f0147.Emscripten.clang
  • Remix Online IDE

    pragma solidity ^0.4.24;
    
    contract TestSomething {
    
        uint256[] public barLengthToBarAmtForSale;
    
        constructor() public{
            uint16 counter = uint16(0);
            while(counter <= 10) {
                barLengthToBarAmtForSale.push(uint256(0));
                counter++;
    
            }
        }
    
        function getVal(uint val) public view returns(uint){
            return barLengthToBarAmtForSale[val]; 
        }
    
        function setVal(uint16 _length) public {
            barLengthToBarAmtForSale[_length]++;
        }
    
        function delVal(uint index) public {
            delete barLengthToBarAmtForSale[index];
        }
    
        function getLen() public view returns(uint){
            return barLengthToBarAmtForSale.length;
        }
    }
    

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.