牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义 [英] Solidity: ParserError: Expected pragma, import directive or contract /interface/library definition

查看:364
本文介绍了牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写简单合约时也遇到了最新的solc(0.5.2版本)和0.4.25错误

I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract

我尝试了以下步骤


  1. 未安装的Solc:npm卸载solc

  2. 已安装的目标版本:npm install --save solc@0.4。 25

  3. 节点compile.js(代码如下)

  1. uninstalled Solc: npm uninstall solc
  2. Installed targeted version: npm install --save solc@0.4.25
  3. node compile.js (code given below)

  { 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

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

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上编译。

Code worked well on Remix, for version 0.5.2 I have added memory tag to make it compile on Remix.

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


推荐答案

solc< = v0.4.25



您的主要使用Solidity / solc v0.4.25 的问题是您的构造函数定义。

solc <= v0.4.25

Your primary issue using Solidity/solc v0.4.25 is your constructor definition.

您当前将构造函数定义为:

You currently have your constructor defined as:

function Inbox(string passedName) public

但是,在Solidity中已弃用了与合同同名的定义构造函数。尝试使用 constructor 关键字定义构造函数。

However, defining constructors with the same name as the contract has been deprecated in Solidity. Try defining your constructor using the constructor keyword instead.

 constructor(string passedName) public

如果您使用的是solc v0.4.25 ,请参阅到文档以便了解如何正确处理将输入传递给 compile 函数。见下面我的参考:

If you are using solc v0.4.25, please refer to the documentation in order to understand how to properly pass input to the compile function. See my reference below:

const input = { 
    'Inbox.sol': fs.readFileSync(path.resolve(__dirname, 'contracts', 'Inbox.sol'), 'utf8') 
}
const output= solc.compile({sources: input}, 1);

if(output.errors) {
    output.errors.forEach(err => {
        console.log(err);
    });
} else {
    const bytecode = output.contracts['Inbox.sol:Inbox'].bytecode;
    const abi = output.contracts['Inbox.sol:Inbox'].interface;
    console.log(`bytecode: ${bytecode}`);
    console.log(`abi: ${JSON.stringify(JSON.parse(abi), null, 2)}`);
}



solc> = v0.5.0



如果使用的是Solidity / solc v0.5.2 ,则还需要修复构造函数的定义。此外,您需要将 memory 关键字添加到每个返回或接受函数中字符串类型。

solc >= v0.5.0

If you are using Solidity/solc v0.5.2, you will also need to fix your constructor definition. Furthermore, you will need to add the memory keyword to each function that returns or accepts the string type.

例如:

function setMessage(string newMsg) public

应声明为:

function setMessage(string memory newMsg) public

更多,请参阅<一个href = https://solidity.readthedocs.io/en/latest/using-the-compiler.html rel = nofollow noreferrer>最新文档,以便了解最新的Solidity编译器之间的差异和旧版本。请参阅下面的参考资料,了解如何使用最新的编译器为 compile 函数定义输入:

Futhermore, please see the latest documentation in order to understand the differences between the latest Solidity compiler and the older version. See my reference below for how to define the input for the compile function utilizing the latest compiler:

const input = { 
    language: "Solidity",
    sources: {
        "Inbox.sol": {
            content: fs.readFileSync(path.resolve(__dirname, "contracts", "Inbox.sol"), "utf8") 
        }
    },
    settings: {
        outputSelection: {
            "*": {
                "*": [ "abi", "evm.bytecode" ]
            }
        }
    }
}
const output = JSON.parse(solc.compile(JSON.stringify(input)));

if(output.errors) {
    output.errors.forEach(err => {
        console.log(err.formattedMessage);
    });
} else {
    const bytecode = output.contracts['Inbox.sol'].Inbox.evm.bytecode.object;
    const abi = output.contracts['Inbox.sol'].Inbox.abi;
    console.log(`bytecode: ${bytecode}`);
    console.log(`abi: ${JSON.stringify(abi, null, 2)}`);
}

这篇关于牢固性:ParserError:预期的编译指示,导入指令或合同/接口/库定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆