1

Thus far I've RTFM'd the Data Location docs, and tried this solution, to no avail.

The full code of the contract looks like this:

pragma solidity ^0.4.13;

contract People {

    Person[] public people;

    struct Person {
        bytes32 firstName;
        bytes32 lastName;
        uint age; 
    }

    function addPerson(bytes32 _firstName, bytes32 _lastName, uint _age) returns (bool success) {

        Person memory newPerson;
        newPerson.firstName = _firstName;
        newPerson.lastName = _lastName;
        newPerson.age = _age;

        people.push(newPerson);
        return true;

    }

    function getPeople() constant returns (bytes32[],bytes32[],uint[]) {

        uint length = people.length;

        bytes32[] memory firstNames = new bytes32[](length);
        bytes32[] memory lastNames = new bytes32[](length);
        uint[] memory ages = new uint[](length);

        for(uint i = 0; i < people.length; i++) {

            // what does <<memory>> do in Solidity?
            Person memory currentPerson;

            currentPerson = people[i];

            firstNames = currentPerson.firstName;
            lastNames = currentPerson.lastName;
            ages = currentPerson.age;
        }

        return (firstNames, lastNames, ages);

    }

}

When I try to compile it with truffle compile --all I get the following error:

Compiling ./contracts/ConvertLib.sol...
Compiling ./contracts/MetaCoin.sol...
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/People.sol...

/Users/s.matthew.english/ConsenSys/PegaSys/smart_contracts/peopleList/0.0_peopleList/contracts/People.sol:40:26: TypeError: Type bytes32 is not implicitly convertible to expected type bytes32[] memory.
        firstNames = currentPerson.firstName;
                     ^---------------------^
,/Users/s.matthew.english/ConsenSys/PegaSys/smart_contracts/peopleList/0.0_peopleList/contracts/People.sol:41:25: TypeError: Type bytes32 is not implicitly convertible to expected type bytes32[] memory.
        lastNames = currentPerson.lastName;
                    ^--------------------^
,/Users/s.matthew.english/ConsenSys/PegaSys/smart_contracts/peopleList/0.0_peopleList/contracts/People.sol:42:20: TypeError: Type uint256 is not implicitly convertible to expected type uint256[] memory.
        ages = currentPerson.age;
               ^---------------^
Compiliation failed. See above.

So it has to do with this assignment apparently:

        firstNames = currentPerson.firstName;
        lastNames = currentPerson.lastName;
        ages = currentPerson.age;

What's wrong with that?

To provide some context, writing this contract is part of completing this tutorial, specifically the contract at ~20:26.

2

Why do you need currentPerson at all? The following code seems to work:

pragma solidity ^0.4.13;

contract People {

Person[] public people;

struct Person {
    bytes32 firstName;
    bytes32 lastName;
    uint age; 
}

function addPerson(bytes32 _firstName, bytes32 _lastName, uint _age) returns (bool success) {

    Person memory newPerson;
    newPerson.firstName = _firstName;
    newPerson.lastName = _lastName;
    newPerson.age = _age;

    people.push(newPerson);
    return true;

}

function getPeople() constant returns (bytes32[],bytes32[],uint[]) {

    uint length = people.length;

    bytes32[] memory firstNames = new bytes32[](length);
    bytes32[] memory lastNames = new bytes32[](length);
    uint[] memory ages = new uint[](length);

    for(uint i = 0; i < people.length; i++) {

        // what does <<memory>> do in Solidity?
        //Person memory currentPerson;
        //currentPerson = people[i];

        firstNames[i] = people[i].firstName;
        lastNames[i] = people[i].lastName;
        ages[i] = people[i].age;
    }

    return (firstNames, lastNames, ages);

}

}

0

I was assigning an array element to an array, had to do this:

    firstNames[i] = currentPerson.firstName;
    lastNames[i] = currentPerson.lastName;
    ages[i] = currentPerson.age;

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.