0

I'm always getting this error: UnimplementedFeatureError: Copying of type struct C.Friends memory[] memory to storage not yet supported. I don't know what is wrong and how to fix it. Can someone help me, please?

pragma solidity >=0.4.22 <0.7.0;
pragma experimental ABIEncoderV2;

contract C {
    
    struct Friends{
        string name;
    }
    
    struct Person{
        string name;
        Friends[] friends;
    }
    
    mapping(string => Person) persons;
    
    
    
    function map(string memory name)public{
        
        
        Person memory person;
        persons[name] = person;
    }
     
}
3
  • From your code, it seems that all you need is persons[name].name = name. – goodvibration Jul 20 '20 at 10:02
  • nope, does not work – Laurens Wissels Jul 20 '20 at 10:29
  • ass soon as I try to add the friends array the error occurs – Laurens Wissels Jul 20 '20 at 10:32
1

Try this:

pragma solidity >=0.4.22 <0.7.0;
pragma experimental ABIEncoderV2;

contract C {
    struct Friends {
        string name;
    }
    
    struct Person {
        string name;
        Friends[] friends;
    }
    
    mapping(string => Person) persons;
    
    function map(string memory _name, string[] memory _friends_names) public {
        persons[_name].name = _name;
        
        uint256 length = _friends_names.length;
        for (uint256 i = 0; i < length; i+=1) {
            persons[_name].friends.push(Friends(_friends_names[i]));
        }
    }
    
    function returnSingleStruct(string memory _name) public view returns(string memory, string[] memory) {
        uint256 length = persons[_name].friends.length;
        string[] memory friends_names = new string[](length);
        for (uint i = 0; i < length; i+=1) {
            friends_names[i] = persons[_name].friends[i].name;
        }
        
        return (persons[_name].name, friends_names);
    }
}

In my example you can insert array of Friends structs for struct Person. I also built getter method which is returning the name of the person including array of the friends.

2
  • So do you indirectly create a struct through persons[_name].name = _name; ? – Laurens Wissels Jul 20 '20 at 10:51
  • Yes. BTW in my example you can see I'm saving _name for struct Person with mapping key _name which is a bit useless, because you're saving _name twice ( you don't have to save the name inside the Person struct, you already did that when you used the name for unique mapping key ). I left it like on purpose for the test, you can save whatever information you want in the struct Person. – Miroslav Nedelchev Jul 20 '20 at 11:09

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.