I am new to solidity. I have a Person struct and mapping:

 struct Person{
        uint age;
        string gender;

 Person[] players; // holds the 24 players

 mapping (string => Person) selectedPair;

I have to chose a boy and a girl.

function choosePair() public {
        // save the chosen person in selectedPair if its the correct gender
        for (uint i=0; i<players.length; i++) {
            Person storage chosenOne = players[pseudoRandom(0,23)]; 
            if(selectedPair[chosenOne.gender] != 0){ //error!
                // I want to know if there is a "girl" already in the selectedPair mapping. 

I get the error:

TypeError: Operator != not compatible with types struct

1 Answer 1


selectedPair is a mapping of strings to Person structs.

That is to say, selectedPair[A_STRING_HERE] will always return a Person struct, regardless of the value of A_STRING_HERE (which has to be a string).

You have defined the Person struct as having one uint property called age, and one string property called gender.

So to get the gender property of the above, you would need to call it like this


But note, we can't directly compare strings. So if you want to compare the gender value against something else, you will need to hash both values and compare their hashes.

   keccak256(abi.encodePacked(selectedPair[A_STRING_HERE].gender)) == keccak256(abi.encodePacked(SOME_OTHER_STRING))

This will resolve the error reported in the question.

  • But the keys in the mapping are not Persons, they are strings like: `{"girl"=Person, "boy"=0}. That would be a selected pair that is missing the boy. I wouild like to know if there is already a "boy", for example. Thanks a lot for your answer. Commented Sep 20, 2019 at 23:38
  • 1
    A mapping of string to Person will never have 0 as a value. If its unassigned, it will be mapped to a Person that has empty values. That is, age=0 and gender="" Commented Sep 21, 2019 at 0:07

Your Answer

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

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