![](/img/trans.png)
[英]Simple smart contract in ethereum remix - ParserError: Expected ';'
[英]Solidity: ParserError: Expected pragma, import directive or contract /interface/library definition
我在编写简单合同时也遇到了最新的 solc(0.5.2 版本)和 0.4.25 错误
我试过以下步骤
节点 compile.js(下面给出的代码)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
收件箱.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。
ex: function setMessage(string **memory** newMsg)
在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误
我试过以下步骤
node compile.js(下面给出的代码)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
收件箱.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。
ex: function setMessage(string **memory** newMsg)
在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误
我试过以下步骤
node compile.js(下面给出的代码)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
收件箱.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。
ex: function setMessage(string **memory** newMsg)
在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误
我试过以下步骤
node compile.js(下面给出的代码)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
收件箱.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。
ex: function setMessage(string **memory** newMsg)
在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误
我试过以下步骤
node compile.js(下面给出的代码)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\\nD:\\\\RND\\\\BlockChain\\\\contracts\\\\Inbox.sol\\n^\\n' ],sourceList: [ '' ],sources: {} }
编译.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inPath = path.resolve(__dirname,'contracts','Inbox.sol');
const src = fs.readFileSync(inPath,'UTF-8');
const res = solc.compile(inPath, 1);
console.log(res);
收件箱.sol
pragma solidity ^0.4.25;
contract Inbox {
string message;
function Inbox(string passedName) public {
message = passedName;
}
function setMessage(string newMsg) public {
message = newMsg;
}
function getMessage() public view returns(string){
return message;
}
}
代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。
ex: function setMessage(string **memory** newMsg)
检查您的 .sol 文件是否以没有 BOM 的 UTF-8 编码。
否则,转换为 UTF-8。
我使用 Notepad++ 来做到这一点。
编码 > 转换为 UTF-8
当您在程序的语法中出错时,就会发生解析错误。 我遇到了同样的错误,因为
contract ERC721Enumerable is ERC721{
function totalSupply() public view returns (uint256){
return _allTokens.length;
}
// THIS WAS THE REASON
// this was closing contract and then outside of the contract i had function
}
function _mint(address to, uint256 tokenId) internal override {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.