到目前为止,我已经对数据定位文档进行了RTFM访问,并尝试了这个解决方案,但没有结果。
合同的完整代码如下所示:
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编译它时,我得到以下错误:
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.很显然,这与这项任务有关:
firstNames = currentPerson.firstName;
lastNames = currentPerson.lastName;
ages = currentPerson.age;这有什么问题吗?
为了提供一些上下文,编写此合同是完成本教程的一部分,特别是在~20:26上的合同。
发布于 2017-08-01 21:01:48
你为什么需要currentPerson呢?以下代码似乎有效:
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);
}}
发布于 2017-08-01 17:34:32
我给数组分配了一个数组元素,必须这样做:
firstNames[i] = currentPerson.firstName;
lastNames[i] = currentPerson.lastName;
ages[i] = currentPerson.age;https://ethereum.stackexchange.com/questions/23481
复制相似问题