预期的";",但得到"构造函数"



大家好,我写了一些坚实的代码,但不幸的是得到了一些错误,是什么事?

我正在使用HardHat编译我的代码,得到下一个错误:

错误看起来像这样:ParserError:期望';',但是得到'constructor'——比;合同/Tree.sol: 14:5

代码是这样的:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Tree {
bytes32[] hashes;
string[4] transactions = [
"first transaction",
"second transaction",
"third transaction",
"fourth transaction"
];
constructor() {
for (uint i = 0; i < transactions.length; i++) {
hashes.push(makeHash(transactions[i]));
}
uint count = transactions.length;
uint offset = 0;
while (count > 0) {
for (uint i = 0; i < count - 1; i += 2) {
hashes.push(
keccak256(
abi.encodePacked(
hashes[offset + 1],
hashes[offset + i + 1]
)
)
);
}
offset += count;
count = count / 2;
}
}
function verify(
string memory transaction,
uint index,
bytes32 root,
bytes32[] memory proof
) public pure returns (bool) {
bytes32 hash = makeHash(transaction);
for (uint i = 0; i < proof.length; i++) {
bytes32 element = proof[i];
if (index % 2 == 0) {
hash = keccak256(abi.encodePacked(hash, element));
} else {
hash = keccak256(abi.encodePacked(element, hash));
}
index = index / 2;
}
return root == hash;
}
function encode(string memory input) public pure returns (bytes memory) {
return abi.encodePacked(input);
}
function makeHash(string memory input) public pure returns (bytes32) {
return keccak256(encode(input));
}
}

我无法重现您的问题。上面提供的代码完全没问题,编译也很好。

然而,它的问题可能是你在编译时错过了在数组字符串transactions末尾的分号;,在上传代码时在这里替换。

最新更新