以太坊solidity编程常见错误(不定期更新)

推荐一个网站https://ethereum.stackexchange.com,里面的问题比较全,百度谷歌不到的可以到里面找找,再找不到就只能看官方文档了https://solidity.readthedocs.io/en/v0.5.0/

 

1、报错:

Expected token Semicolon got 'eth_compileSolidity' funtion setFunder(uint _u,uint _amount){

解决:

funtion关键字错了,需要用function;

 

2、报错:

Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning. Funder f = funders[_u]; ^------^

解决:

Funder f,定义指针需要加关键字storage ;修改为Funder storage f = funders[_u];

 

3、报错:

Invoking events without "emit" prefix is deprecated. e("newFunder",_add,_amount); ^-------------------------^

解决:

调用事件需要在前面加上emit关键字,修改为emit e("newFunder",_add,_amount);

 

4、报错:

No visibility specified. Defaulting to "public". function newFunder(address _add,uint _amount) returns (uint){ ^ (Relevant source part starts here and spans across multiple lines).

解决:

定义函数必须加上public关键字,修改为function newFunder(address _add,uint _amount) public returns (uint){

 

5、报错:

"msg.gas" has been deprecated in favor of "gasleft()" uint public _gas = msg.gas; ^-----^

 

解决:msg.gas已经被gasleft()替换了。修改为uint public _gas = gasleft();

6、报错:

"throw" is deprecated in favour of "revert()", "require()" and "assert()". throw ;

解决:solidity已经不支持thorw了,需要使用require,用法require()

throw 写法:

if(msg.sender !=chairperson ||voters[_voter].voted ){

throw ;

}

require写法:

require(msg.sender !=chairperson ||voters[_voter].voted);

 

7、报错:

This declaration shadows an existing declaration. Voter delegate = voters[to]; ^------------^

解决:变量重复定义,变量名和函数名不能相同。

 

8、报错:

error: Function state mutability can be restricted to pure

解决:以前版本是可以不指定类型internal pure(外部不可调用),public pure(外部可调用)(如不指定表示函数为可变行,需要限制)

 

 

9、报错:

"sha3" has been deprecated in favour of "keccak256"

解决:sha3已经替换为keccak256

 

10、报错:

 browser/Untitled.sol:95:16: TypeError: Operator != not compatible with types address and int_const 0

assert(_to != 0x0);

解决:新版本的solidity,地址变量不能和0x0进行比较,修改为 assert(_to != address(0x0));

 

11、报错

browser/Untitled.sol:148:3: TypeError: Member "transfer" not found or not visible after argument-dependent lookup in address.
        owner.transfer(amount);

解决:solidity 0.5,address地址类型细分为 address和 address payable,只有 address payable可以使用 transfer(), send()函数.

原owner定义address public owner ;

修改为address payable public owner ;

12、报错

browser/Untitled.sol:152:2: TypeError: Fallback function must be defined as "external".
    function() payable{
 ^ (Relevant source part starts here and spans across multiple lines).
 解决:solidity 0.5 ,回退函数(fallback function), 接口(interface)的函数必须声明为 external可见性,否则编译报错.

修改为function() payable external{

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值