Solidity ERC721A合约编译报ParserError语法错误解决方案
Solidity编译报错:ParserError: Expected '{' but got identifier
报错输出
编译合约时抛出如下错误:
from solidity: ParserError: Expected '{' but got identifier --> Mint.sol:1443:16: | 1443 | contract Eight Founding Fathers is ERC721A, Ownable, ReentrancyGuard { | ^^^^^^^^
出错代码片段
//below is my code// // line 1443 ignore this// contract Eight Founding Fathers is ERC721A, Ownable, ReentrancyGuard { string public baseURI; uint public price = 0.003 ether; uint public maxPerTx = 20; uint public totalFree = 1000; uint public maxSupply = 1776; uint256 public maxFreePerWallet = 3; bool public mintEnabled; mapping(address => uint256) private _mintedFreeAmount; constructor() ERC721A("8Bit Founding Fathers","8BFF"){} }
根因分析
Solidity语法规定,合约名称属于标识符,必须满足标识符命名规则:只能由字母、数字、下划线组成,不能包含空格,且不能以数字开头。
当前代码里合约名写的是Eight Founding Fathers,中间带空格,编译器解析到第一个单词Eight后,判定合约名已经结束,按照语法规则下一个标记应该是表示合约体开始的左大括号{,结果读到了独立的标识符Founding,直接触发解析报错。
注意:构造函数里传入的
"8Bit Founding Fathers"是字符串类型的NFT集合名称,不属于标识符,允许包含空格,这部分代码没有问题,不需要修改。
修复方案
把合约定义行的名称改成无空格的合法命名即可,示例修改:
// 修改前:contract Eight Founding Fathers is ERC721A, Ownable, ReentrancyGuard { // 修改后: contract EightFoundingFathers is ERC721A, Ownable, ReentrancyGuard {
修改完成后重新编译即可解决该报错。
内容的提问来源于stack exchange,提问作者Miles Brown
相关产品推荐
相关产品推荐

