我试图将一个struct传递给合同的构造函数,但是得到了错误。
TypeError: Internal or recursive type is not allowed for public or external functions合同是
pragma solidity ^0.4.0;
contract Writer {
struct Paragraph {
string[] lines;
}
struct Essay {
Paragraph[] paragraphs;
}
Essay[] private essays;
function Writer(Essay initialEssay) {
essays.push(initialEssay);
}
}我做了一些调查,问题似乎并不是实际上与在契约中定义的struct有关(毕竟在哪里还会定义它),而是使用嵌套数组,所以我将它更改为
contract Writer {
struct Paragraph {
string sentances;
}
struct Essay {
string title;
Paragraph[] paragraphs;
}
Essay[] private essays;
function Writer(Essay essay) {
essays.push(essay);
}
}但现在我得到了
InternalCompilerError: Static memory load of more than 32 bytes requested.因此,我尝试了这种方式,手动构建struct。
contract Writer {
struct Paragraph {
string sentances;
}
struct Essay {
string title;
Paragraph[] paragraphs;
}
Essay[] private essays;
function Writer(string title, string[] _paras) {
Paragraph[] storage paras;
for (uint256 i = 0; i < _paras.length; i++) {
Paragraph memory para = Paragraph(_paras[i]);
paras.push(para);
}
Essay memory initialEssay = Essay(title, paras);
essays.push(initialEssay);
}
}这给
UnimplementedFeatureError: Nested arrays not yet implemented.我是否应该放弃使用struct,而定义一个新的契约来表示Essay呢?
发布于 2017-10-25 11:59:04
如果嵌套数组是外部函数,则不能将其作为函数参数传递。当您试图传递_paras时,它将失败,因为string[]是一个嵌套数组。
对于您的问题,最简单的解决方案是,如果不是很多参数,只需将每个参数作为单独的参数传递,在函数中构造结构并将其推送到数组中即可。
这就像您尝试过的最后一个实现,但是如果pos传递string[] params,则会传递字符串param1、string param2、string param3等等。
发布于 2023-01-08 08:27:39
解决方案是先创建一个引用,然后赋值。
uint256 idx = essays.length;
essays.push();
Essay storage e= essays[idx];
e.paragraphs.push(Paragraph("your sentense");https://ethereum.stackexchange.com/questions/29217
复制相似问题