0

I have two solidity files named SimpleStorage and StorageFactory. SimpleStorage file contains the functions that store and retrieve the users' name and age. The other solidity file StorageFactory deploys the contract and extract the functions.

SimpleStorage.sol

//SPDX-License-Identifier: MIT

pragma solidity 0.8.8;

contract SimpleStorage{

    struct People{
        string name;
        uint256 age;
    }

    People[] person;

    function setPerson(string memory _name, uint256 _age) public{
        People memory newPerson = People({name:_name, age:_age});
        person.push(newPerson);
    }

    function getPerson() public view returns (People[] memory){
        return person;
    }
}

StorageFactory.sol

//SPDX-License-Identifier: MIT

pragma solidity 0.8.8;

import "./SimpleStorage.sol";

contract StorageFactory{

SimpleStorage[] simpleStorageArray;

 struct People{
     string name;
     uint256 age;
 }

 People[] people;

function createSimpleStorageContract() public {
    SimpleStorage simpleStorage = new SimpleStorage();
    simpleStorageArray.push(simpleStorage);
}

function sfSetPerson(uint256 contractAddressIndex, string memory _name, uint256 _age) public {
    SimpleStorage simpleStorage = simpleStorageArray[contractAddressIndex];
    simpleStorage.setPerson(_name, _age);
}

function sfGetPerson(uint256 contractAddressIndex) public view returns (People[] memory){
    SimpleStorage  simpleStorage = simpleStorageArray[contractAddressIndex];
    return simpleStorage.getPerson();
}

}

I want to call the function getPerson which returns an array from the StorageFactory.sol but getting the error:

from solidity: TypeError: Return argument type struct SimpleStorage.People memory[] memory is not implicitly convertible to expected type (type of first return variable) struct StorageFactory.People memory[] memory. --> contracts/StorageFactory.sol:30:12: | 30 | return simpleStorage.getPerson();

1 Answer 1

0

This error happen because you declared two structs with the same name, in this case: Person. Thus, in sfGetPerson() method you must declare inside returns keyword the contract name and its relative struct name. You must change this function in this way:

function sfGetPerson(uint256 contractAddressIndex) public view returns (SimpleStorage.People[] memory){
        SimpleStorage simpleStorage = simpleStorageArray[contractAddressIndex];
        return simpleStorage.getPerson();
}

In the following lines, I put your StorageFactory smart contract modified:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.8;

import "./SimpleStorage.sol";

contract StorageFactory{
    SimpleStorage[] simpleStorageArray;

    struct People{
        string name;
        uint256 age;
    }

    People[] people;

    function createSimpleStorageContract() public {
        SimpleStorage simpleStorage = new SimpleStorage();
        simpleStorageArray.push(simpleStorage);
    }

    function sfSetPerson(uint256 contractAddressIndex, string memory _name, uint256 _age) public {
        SimpleStorage simpleStorage = simpleStorageArray[contractAddressIndex];
        simpleStorage.setPerson(_name, _age);
    }

    function sfGetPerson(uint256 contractAddressIndex) public view returns (SimpleStorage.People[] memory){
        SimpleStorage simpleStorage = simpleStorageArray[contractAddressIndex];
        return simpleStorage.getPerson();
    }

}

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.