Solidity:ParserError:预期的编译指示、导入指令或合约/接口/库定义

yog*_*rma 4 node.js blockchain ethereum solidity smartcontracts

当我编写简单合约时,我也遇到了最新的 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: {} }
    
    Run Code Online (Sandbox Code Playgroud)

编译.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);
Run Code Online (Sandbox Code Playgroud)

收件箱.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

代码在 Remix 上运行良好,对于 0.5.2 版本,我添加了内存标签以使其在 Remix 上编译。

ex:   function setMessage(string **memory** newMsg) 
Run Code Online (Sandbox Code Playgroud)

joe*_*vid 9

您的情况并非如此,但我将把这个解决方案留在这里给可能需要它的人。当我忘记分号 ';' 时出现此错误 在第一行的末尾pragma solidity ^0.4.25;。所以一定要检查一下。