简体   繁体   English

Solidity:ParserError:预期的编译指示、导入指令或合同/interface/library 定义

[英]Solidity: ParserError: Expected pragma, import directive or contract /interface/library definition

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract我在编写简单合同时也遇到了最新的 solc(0.5.2 版本)和 0.4.25 错误

I have tried following steps我试过以下步骤

  1. uninstalled Solc: npm uninstall solc卸载的 Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25安装的目标版本: npm install --save solc@0.4.25
  3. node compile.js (code given below)节点 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: {} }

Compile.js编译.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);

Inbox.sol收件箱.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;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误

I have tried following steps我试过以下步骤

  1. uninstalled Solc: npm uninstall solc卸载的 Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25安装的目标版本: npm install --save solc@0.4.25
  3. node compile.js (code given below) 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: {} }

Compile.js编译.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);

Inbox.sol收件箱.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;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误

I have tried following steps我试过以下步骤

  1. uninstalled Solc: npm uninstall solc卸载的 Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25安装的目标版本: npm install --save solc@0.4.25
  3. node compile.js (code given below) 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: {} }

Compile.js编译.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);

Inbox.sol收件箱.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;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误

I have tried following steps我试过以下步骤

  1. uninstalled Solc: npm uninstall solc卸载的 Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25安装的目标版本: npm install --save solc@0.4.25
  3. node compile.js (code given below) 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: {} }

Compile.js编译.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);

Inbox.sol收件箱.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;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract在编写简单合同时,最新的 solc(0.5.2 版本)和 0.4.25 也出现错误

I have tried following steps我试过以下步骤

  1. uninstalled Solc: npm uninstall solc卸载的 Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25安装的目标版本: npm install --save solc@0.4.25
  3. node compile.js (code given below) 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: {} }

Compile.js编译.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);

Inbox.sol收件箱.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;
    }
}

Code worked well on Remix , for version 0.5.2 I have added memory tag to make it compile on Remix.代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 

Check if your .sol file is encoded in UTF-8 without BOM.检查您的 .sol 文件是否以没有 BOM 的 UTF-8 编码。

Otherwise, convert to UTF-8.否则,转换为 UTF-8。

I used Notepad++ to do that.我使用 Notepad++ 来做到这一点。

Encoding > Convert to UTF-8编码 > 转换为 UTF-8

Parse errors happen when you make an error in the syntax of your program.当您在程序的语法中出错时,就会发生解析错误。 I got the same error because我遇到了同样的错误,因为

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM