我正在尝试做一个dapp项目。
我有一个关于堆栈太深的错误,但我不知道如何解决这个问题。
CompilerError: Stack too deep, try remove local variables。——比;合同/proje.sol: 145:5:|145 |函数addRecord(地址_addr, s…)gedOn,字符串内存ipfs) public {| ^(相关的来源部分从这里开始,跨越多行)。
实际上,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结构体:
function addRecord(address _addr, Records memory record) public {
// your logic
}
另一个解决这个问题的方法是将函数类型从public改为internal,比如: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
}
注意:在进行此修改之前,请在这里查看公共类型和内部类型函数之间的区别。