首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Solidity : uint256不能隐式转换为uint256[]内存

Solidity : uint256不能隐式转换为uint256[]内存
EN

Ethereum用户
提问于 2017-08-01 17:18:34
回答 2查看 2.2K关注 0票数 1

到目前为止,我已经对数据定位文档进行了RTFM访问,并尝试了这个解决方案,但没有结果。

合同的完整代码如下所示:

代码语言:javascript
复制
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);

    }

}

当我尝试用truffle compile --all编译它时,我得到以下错误:

代码语言:javascript
复制
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.

很显然,这与这项任务有关:

代码语言:javascript
复制
        firstNames = currentPerson.firstName;
        lastNames = currentPerson.lastName;
        ages = currentPerson.age;

这有什么问题吗?

为了提供一些上下文,编写此合同是完成本教程的一部分,特别是在~20:26上的合同。

EN

回答 2

Ethereum用户

回答已采纳

发布于 2017-08-01 21:01:48

你为什么需要currentPerson呢?以下代码似乎有效:

代码语言:javascript
复制
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);

}

}

票数 2
EN

Ethereum用户

发布于 2017-08-01 17:34:32

我给数组分配了一个数组元素,必须这样做:

代码语言:javascript
复制
    firstNames[i] = currentPerson.firstName;
    lastNames[i] = currentPerson.lastName;
    ages[i] = currentPerson.age;
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/23481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档