我在一个坚固的项目中工作 - 我似乎可以为此提供解决方案:在struct中使用struct类型

发布于 2025-02-10 06:01:33 字数 1306 浏览 2 评论 0原文

我正在尝试这样做:要注册处方我创建2个结构:struct Prescstruct 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-02-17 06:01:33

您正在尝试访问drug类型结构的索引。

基于代码的上下文,您可能想访问drugss变量(即drug> drug)。

addDrugs(_id, drugss[_id].name, drugss[_id].Qtt);

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 type Drug).

addDrugs(_id, drugss[_id].name, drugss[_id].Qtt);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文