11

I have a mapping(string => Person) public map:

struct Person {
        string name;
        string description;
        address primaryAddress;
        string linkToWebsite;
        string linkToPicture;
        address secondaryAddress;
        uint age;
}

, this struct is only made of strings, addresses and uints, but when compiling I get this error.

Internal compiler error: Accessors for mapping with dynamically-sized keys not yet implemented.

BUT,

From Solidity Features:

Strings as Mapping Keys

Strings are allowed as keys for mappings.

contract C {   mapping (string => uint) counter;   
function inc(string _s) { counter[_s]++; } }

Any help with it?

3 Answers 3

21

The thing was that my mapping was public, and there are no public accessors implemented for it yet. The solution is just to not declare the mapping as public.

2
  • Why do you need a public accessor? Shouldn't it generate the getter? "It is possible to mark mappings public and have Solidity create a getter. The _KeyType will become a required parameter for the getter and it will return _ValueType." source Mar 6, 2018 at 20:18
  • 1
    I declared my mapping as private to solve this
    – Alex
    Mar 10, 2018 at 10:19
5

Strings are allowed as keys in mapping. For example, this code works just fine testing in Pyethereum:

 from ethereum import tester as t

 code = '''
    contract Example {
       struct Person {
         string name;
       }
       mapping(string => Person) map;

       function setName(string name, string value) {
          map[name] = Person({name: value});
       }

       function getName(string key) constant returns (string) {
         return map[key].name;
       }

   }
  '''

   state = t.state()
   contract =  state.abi_contract(code, language='solidity')
   contract.setName('dave', 'hello')
   print contract.getName('dave')
   => 'hello'

There must be something going on in your actual contract code

4
  • what can make this error to appear? Mar 29, 2016 at 15:41
  • I don't know without seeing the whole contract. Are you using a newer version of the solidity compiler?
    – dbryson
    Mar 29, 2016 at 16:40
  • The most recently one Mar 29, 2016 at 16:41
  • I would recommend working through incremental, simpler, versions of your contract and compiling until you find the problem. Try my code above for starters to see if it works in your setup
    – dbryson
    Mar 29, 2016 at 16:47
1

without seeing your struct it's hard to guess, is this helping ?

contract structtestmapping {
    struct myStruct {
        uint id;
    }
    mapping(string => myStruct) mapmystruct;
    myStruct s = mapmystruct['mystring'];

}

your struct might contain dynamically sized elements as the code here suggests ?

8
  • this is not helpful for me, I want to access to the mapping with a string key which is given by parameter, but thank you anyway. I have strings and addresses in the struct. Mar 29, 2016 at 14:35
  • a string is considered a dynamically sized element? Mar 29, 2016 at 15:33
  • I think so if this is correct, wouldn't bet my hand on it though github.com/ethereum/wiki/wiki/… : Re-introduce string type PT string is added as a type which behaves exactly like bytes with the following differences: index access is not allowed it does not have a length member In the ABI encoding (wiki already changed), string is a dynamic type whose "number of elements" field is the number of bytes, not the number of characters. The encoding of the string is assumed to be UTF-8, but is not yet used inside Solidity.
    – euri10
    Mar 29, 2016 at 15:45
  • Is it possible then to fix the size of the strings in my struct? Mar 29, 2016 at 15:47
  • Also @dbryson has a string in his struct and his solution works Mar 29, 2016 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.