简体   繁体   English

Solidity 错误:: ParserError: Expected '(' but got 'public'

[英]Solidity Error :: ParserError: Expected '(' but got 'public'

I keep getting this error.......我不断收到此错误.......

"from solidity:
ParserError: Expected '(' but got 'public'
 --> contracts/3_Ballot.sol:6:24:
  |
6 |     function PiggyBank public()
  |                        ^^^^^^", 

what to do?该怎么办?

    pragma solidity ^0.8.0;
    contract PiggyBank{
    address creator;
    uint deposits;

    function PiggyBank public()          \\error in this line
    {
        creator=msg.sendor;
        deposits=0;
    }

    function deposit() payable returns(uint)
    {
        if(msg.value>0)
        deposits=deposits+1;
        return getNumberofDeposits();  
    }

    function getNumberofDeposits() constant returns(uint)
    {
        return deposit;
    }
    
    function Killl();{
    if(msg.sendor==creator)
    selfdestruct(creator);
    }
}

You're using syntax from older solidity versions, which is not supported in the current 0.8 version.您使用的是旧版本的语法,当前 0.8 版本不支持该语法。

First, to get rid of the syntax error, replace function PiggyBank public() to function PiggyBank() public .首先,要消除语法错误,请将function PiggyBank public()替换为function PiggyBank() public

In older versions of Solidity (up to 0.5), a function with the same name as the contract was used as a constructor.在旧版本的 Solidity(最高 0.5)中,使用与合约同名的 function 作为构造函数。 In the current version 0.8, you need to use the constructor keyword - otherwise the function would be publicly invokable by anyone and anytime (not just during the contract deployment).在当前版本 0.8 中,您需要使用constructor关键字 - 否则 function 将被任何人随时随地公开调用(不仅仅是在合约部署期间)。

// replace `function PiggyBank public()` to `constructor()`
constructor() {
    creator=msg.sendor;
    deposits=0;
}

Then few more syntax errors (incorrectly defined functions; an extra semicolon at the Killl() function definition; missing visibility modifiers), a type error ( selfdestruct argument needs to be payable ), and typos (your code uses msg.sendor instead of msg.sender ) show up.然后还有一些语法错误(错误定义的函数; Killl() function 定义中的额外分号;缺少可见性修饰符)、类型错误(需要payable selfdestruct参数)和拼写错误(您的代码使用msg.sendor而不是msg.sender ) 出现。 See the corrected code:请参阅更正后的代码:

pragma solidity ^0.8.0;

contract PiggyBank {
    address creator;
    uint deposits;

    constructor() {
        creator=msg.sender;
        deposits=0;
    }

    function deposit() public payable returns(uint) {
        if(msg.value>0) {
            deposits=deposits+1;
        }
        return getNumberofDeposits();  
    }

    function getNumberofDeposits() public view returns(uint) {
        return deposits;
    }
    
    function Killl() public {
        if(msg.sender==creator) {
            selfdestruct(payable(creator));
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Solidity ParserError:预期的标识符,但得到了 '(' constructor() public { ^ - Solidity ParserError: Expected identifier but got '(' constuctor() public { ^ 坚固性:ParserError:预期';' 但得到'(' - solidity: ParserError: Expected ';' but got '(' Solidity ParserError:预期的';' 但得到了“是” - Solidity ParserError: Expected ';' but got 'is' "Solidity 错误 :: ParserError: Expected ',' but got 'Number'" - Solidity Error :: ParserError: Expected ',' but got 'Number' 无法解决 Solidity 错误 - ParserError: Expected ';' 但得到了“数字”-> first.sol:2:17: | 2 | Pragma 坚固性 0.8.7; | ^^^ - Unable to solve solidity error- ParserError: Expected ';' but got 'Number' --> first.sol:2:17: | 2 | Pragma solidity 0.8.7; | ^^^ SOLIDITY:[函数覆盖]:-> ParserError:预期为“{”但得到保留关键字“覆盖” - SOLIDITY : [Function Overridding] : -> ParserError: Expected '{' but got reserved keyword 'override' 解析器错误:应为“;” 但得到了标识符 - ParserError: Expected ';' but got identifier ParserError:应为“;” 但得到了保留关键字“in” - ParserError: Expected ';' but got reserved keyword 'in' 得到 parsererror 我在 remix 上写 inheritance solidity 代码我得到了错误 - getting parsererror I write inheritance solidity code on remix i got error 采取solidity课程,我被卡住了,因为我的代码不断带来这个错误 ParserError: Function, variable, struct or modifier declaration expected - taking solidity course and i am stuck cause my code keeps bring this error ParserError: Function, variable, struct or modifier declaration expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM