0

Is it possible to do something similar to the below? Currently when compiling it will throw an error saying that "Name has to refer to a struct, enum or contract". I also have a "working?" solution further below but I am unsure which would be the proper way.

mapping(address => uint256) shelf;
mapping (address => shelf) bookcase;

The idea being that an address would point to a bookcase which goes to a shelf which goes to an int (number of books).

The below works fine so far for setting the value for the shelf. The problem in this version is that I can not make bookcase public and can't figure out how to create a getter.

struct shelf
{
  mapping(address => uint256) shelves;      
}
mapping (address => shelves) bookcase;
bookcase[msg.sender] = shelf();
//shelf_address is argument from the function this code is in
bookcase[msg.sender].shelves[shelf_address] = 1;

Thanks for the pointers.

1

This is how to use nested mappings and structs within a mapping.

Accesible through bookcase[--address--][--index--].books / bookcase[--address--][--index--].shelfId In real life you would make the second mapping an array though.

pragma solidity ^0.4.10;

contract Mappings {
    mapping(address => mapping(uint256 => Shelf)) bookcase;
    struct Shelf {
      bytes32[] books;
      uint shelfId;
    }
}

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.