簡體   English   中英

Solidity:ParserError:預期的編譯指示、導入指令或合同/interface/library 定義

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

我在編寫簡單合同時也遇到了最新的 solc(0.5.2 版本)和 0.4.25 錯誤

我試過以下步驟

  1. 卸載的 Solc: npm uninstall solc
  2. 安裝的目標版本: npm install --save solc@0.4.25
  3. 節點 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 也出現錯誤

我試過以下步驟

  1. 卸載的 Solc: npm uninstall solc
  2. 安裝的目標版本: npm install --save solc@0.4.25
  3. 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 也出現錯誤

我試過以下步驟

  1. 卸載的 Solc: npm uninstall solc
  2. 安裝的目標版本: npm install --save solc@0.4.25
  3. 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 也出現錯誤

我試過以下步驟

  1. 卸載的 Solc: npm uninstall solc
  2. 安裝的目標版本: npm install --save solc@0.4.25
  3. 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 也出現錯誤

我試過以下步驟

  1. 卸載的 Solc: npm uninstall solc
  2. 安裝的目標版本: npm install --save solc@0.4.25
  3. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM