8

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?

3 Answers3

14

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.

hrkrshnn
  • 426
  • 3
  • 9
0

Just keep the mapping outside the struct. you do not have to keep inside the struct

     mapping(uint => address) poolUserList;
Yilmaz
  • 1,580
  • 10
  • 24
0

A mapping can only be returned in a struct in a function, if the function is internal or when using a library.