3

I've been coding in solidity and got this error "Member "betsOnBitcoinGoesUp" is not available in struct BetOnBitcoinNew.BettingObject memory outside of storage." What does this mean?

The function that produced error looks like this.

function getUserBetsfromBettingObject(uint index, address callerAddress) constant returns (uint) {
BettingObject memory currentBettingObject = arrayOfBettingObjects[index];
if (currentBettingObject.currentState == GameState.bitcoinUpWon) {
  return currentBettingObject.betsOnBitcoinGoesUp[callerAddress];
} else if (currentBettingObject.currentState == GameState.bitcoinDownWon) {
  return currentBettingObject.betsOnBitcoinGoesDown[callerAddress];
} else {
  return 0;
}

}

2 Answers2

4

As MSwezey states. The issue is because currentBettingObject is declared as BettingObject memory, not BettingObject storage.

Memory in the EVM is an array. Storage is a key value map (aka mapping). A map can not be represented as an array as they are fundamentally different data structures. Therefore Solidity mapping's only exist in storage they cannot exist in memory.

When doing BettingObject memory currentBettingObject = arrayOfBettingObjects[index]; you copy a BettingObject from storage into memory but since the mapping in the BettingObject can't be represented in the memory structure available (an array), it isn't copied and you get the error when trying to access it. If you replace that line with BettingObject storage currentBettingObject = arrayOfBettingObjects[index]; it will work since you're accessing the BettingObject in storage directly without copying it.

willjgriff
  • 1,658
  • 8
  • 13
1

I imported into my VS Code and got the same error.

The problem lies with the memory keyword used. Switching it to storage fixes this problem and the compiler error goes away.

Why?

See Roland's answer: Working with structure arrays in solidity

Matt Swezey
  • 1,246
  • 6
  • 13