[英]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.