CompilerError: Stack too deep, try removing local variables

CompilerError: Stack too deep, try removing local variables

我正在尝试做一个dapp项目。

我有一个堆栈太深的错误,但我不知道如何解决这个问题。

CompilerError: Stack too deep, try removing local variables. --> contracts/proje.sol:145:5: | 145 | function addRecord(address _addr, s ... gedOn, string memory ipfs) public { | ^ (Relevant source part starts here and spans across multiple lines).

实际上,struct 将包含比现在更多的数据。如果这样给出的数据个数有误,我们要输入的数据太多了怎么办?

contract Patient is Clinic {
    uint256 public p_index = 0;

    struct Records {
        string cname;

        string l_cadence;
        string r_cadence;
        string n_cadence;

        string l_dsupport;
        string r_dsupport;
        string n_dsupport;

        string l_footoff;
        string r_footoff;
        string n_footoff;

        string l_steptime;
        string r_steptime;
        string n_steptime;


        string admittedOn;
        string dischargedOn;
        string ipfs;
    }

    struct patient {
        uint256 id;
        string name;
        string phone;
        string gender;
        string dob;
        string bloodgroup;
        string allergies;
        Records[] records;
        address addr;
    }

    address[] private patientList;
    mapping(address => mapping(address=>bool)) isAuth;
    mapping(address=>patient) patients;
    mapping(address=>bool) isPatient;

    function addRecord(address _addr, string memory cname, string memory l_cadence, string memory r_cadence, string memory n_cadence, string memory l_dsupport, string memory r_dsupport, string memory n_dsupport, string memory l_footoff, string memory r_footoff, string memory n_footoff, string memory l_steptime, string memory r_steptime, string memory n_steptime, string memory admittedOn, string memory dischargedOn, string memory ipfs) public {
        
    }
}

编译器错误是关于 addRecord() 函数的参数号。 要解决这个问题,可以直接使用 Records struct 这样:

function addRecord(address _addr, Records memory record) public {
    // your logic    
}

解决这个问题的另一个选择是将类型函数从 public 更改为内部函数,例如:

function addRecord(address _addr, string memory cname, string memory l_cadence, string memory r_cadence, string memory n_cadence, string memory l_dsupport,
     string memory r_dsupport, string memory n_dsupport, string memory l_footoff, string memory r_footoff, string memory n_footoff, string memory l_steptime, 
     string memory r_steptime, string memory n_steptime, string memory admittedOn, string memory dischargedOn, string memory ipfs) internal {
        // your logic
    }

注意: 在进行此修改之前,请查看 public 和内部类型函数 here.

之间的区别