我正忙着做一个项目。所以我在另一个结构中有一个结构数组。在这种情况下,例如牛中的CattleHealth数组。在我的RecordHealth函数中,我使用牛结构的地址并添加当前列表的当前长度来创建唯一的地址。这样做对吗?
contract WagyuRecordContract
{
address owner;
struct Cattle
{
address RFID;
string Name;
uint256 Weight;
string Gender;
string Colour;
string Breed;
uint Age;
uint DOB;
string Location;
bool Parent;
string SireName;
string DamName;
bool Active;
bool ForSale;
CattleHealth[] HealthRecord;
CattleGrowth[] GrowthRecord;
CattleMovements[] MovementsRecord;
Facility SlaughterDetails;
Meat[] DistributionDetails
}
struct CattleGrowth
{
uint DateRecorded;
uint256 FoodIntake;
uint256 Growth;
}
mapping (address => Cattle) public cattle;
mapping (address=> CattleGrowth) public growth;
modifier Owner()
{
require(msg.sender == owner);
_;
}
function RecordHealth(address rfid, string _bodyCond, string _healthStat, uint256, string _med) Owner public
{
health[rfid+cattle[rfid].HealthRecord.length].DateRecorded = now;
health[rfid+cattle[rfid].HealthRecord.length].BodyCondition = _bodyCond;
health[rfid+cattle[rfid].HealthRecord.length].HealthStatus = _healthStat;
health[rfid+cattle[rfid].HealthRecord.length].Medication = _med;
cattle[rfid].HealthRecord.push(health[rfid+cattle[rfid].HealthRecord.length]);
}
}编辑:我的伙伴在没有使用映射的情况下使用了下面的方法。我不明白这是怎么回事。-1是干什么用的?它为什么对牛来说是独一无二的呢?
function RecordHealth(address rfid, string _bodyCond, string _healthStat, string _med) Owner public
{
cattle[rfid].HealthRecord.push(now, _bodyCond, _healthStat, _med)-1;
}发布于 2018-09-16 16:14:03
不是。虽然在C语言或类似语言中指针算法是正常的规则,但在稳健性中,它不能保证工作,无论是对于最终的包装和最终的循环不对齐。因此,强烈建议不要这样做。
https://ethereum.stackexchange.com/questions/58846
复制相似问题