I'm trying to use a mapping inside a struct:
struct PoolStruct {
uint currentUserID;
uint activeUserID;
uint price;
uint minimalReferrals;
mapping(uint => address) poolUserList;
}
the code compiles, so i guess it's somehow permitted.
The problem starts when i try to assign to that property:
PoolStruct memory pool;
pool = PoolStruct({
currentUserID: 1,
activeUserID: 1,
price: POOL_PRICES[i],
minimalReferrals: POOL_MINIMAL_REFERRALS[i]
});
pool.poolUserList[1] = msg.sender;
get an error on the last line…
TypeError: Member "poolUserList" is not available in struct Definitions.PoolStruct memory outside of storage.
pool.poolUserList[currUserID] = msg.sender;
^---------------^
Is there any way to make this work?
Best Answer
The problem is that mappings can only live in storage. When you define
PoolStruct memory pool;
, the mapping member cannot be created in memory, and therefore the memory struct should be treated as if the mapping member never existed (for solidity < 0.7.0).Starting from solidity 0.7.0, the line
PoolStruct memory pool
will produce an error saying that structs containing (nested) mappings must have storage as data location.