我在一个坚固的项目中工作 - 我似乎可以为此提供解决方案:在struct中使用struct类型
我正在尝试这样做:要注册处方我创建2个结构:struct Presc
和struct drug
我将在添加处方中使用,但是当我创建函数addPrescription
。
错误在adddrugs
函数呼叫:
addDrugs( _id,Drug[_id].name,Drug[_id].Qtt); // TypeError: Integer constant expected
pragma solidity ^0.8.0;
contract Pharm {
struct Presc {
uint id;
uint ref;
uint nbOfDrugs;
string Pname;
Drug[] drugs;
}
struct Drug {
uint id;
string name;
uint Qtt;
}
Drug[] drugss;
mapping(uint=> Presc) public mapPresc;
mapping(uint=> Drug) public mapDrug;
function addDrugs(uint _id, string memory _name, uint _Qtt) public {
_id=1;
Drug storage drgs= mapDrug[_id];
drgs.id=_id;
drgs.name=_name;
drgs.Qtt=_Qtt;
_id++;
}
function addPresc(uint _ref, uint _nbOfDrugs, string memory _Pname, Drug memory _list) public {
uint _id = 1;
Presc storage presc = mapPresc[_id];
for(_id=1; _id <= _nbOfDrugs; _id++){
addDrugs(_id, Drug[_id].name, Drug[_id].Qtt);
}
presc.id=_id;
presc.ref=_ref;
presc.nbOfDrugs=_nbOfDrugs;
presc.Pname=_Pname;
}
}
I'm trying to do this: to register a prescription I create 2 structs: struct Presc
and struct Drug
that I will use in adding prescription but it doesn't work when I create the function addPrescription
.
The error is in addDrugs
function call:
addDrugs( _id,Drug[_id].name,Drug[_id].Qtt); // TypeError: Integer constant expected
pragma solidity ^0.8.0;
contract Pharm {
struct Presc {
uint id;
uint ref;
uint nbOfDrugs;
string Pname;
Drug[] drugs;
}
struct Drug {
uint id;
string name;
uint Qtt;
}
Drug[] drugss;
mapping(uint=> Presc) public mapPresc;
mapping(uint=> Drug) public mapDrug;
function addDrugs(uint _id, string memory _name, uint _Qtt) public {
_id=1;
Drug storage drgs= mapDrug[_id];
drgs.id=_id;
drgs.name=_name;
drgs.Qtt=_Qtt;
_id++;
}
function addPresc(uint _ref, uint _nbOfDrugs, string memory _Pname, Drug memory _list) public {
uint _id = 1;
Presc storage presc = mapPresc[_id];
for(_id=1; _id <= _nbOfDrugs; _id++){
addDrugs(_id, Drug[_id].name, Drug[_id].Qtt);
}
presc.id=_id;
presc.ref=_ref;
presc.nbOfDrugs=_nbOfDrugs;
presc.Pname=_Pname;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试访问
drug
类型结构的索引。基于代码的上下文,您可能想访问
drugss
变量(即drug> drug
)。You're trying to access an index of the
Drug
type struct.Based on the context of your code, you probably wanted to access the
drugss
variable (that is of typeDrug
).