关于solidity开发时遇到的VM Exception while processing transaction: invalid opcode问题,我的代码如下:
pragma solidity ^0.4.16;
contract modifierTest{
uint a=0;
address owner;
constructor() public{
owner = msg.sender;
}
modifier onlyOwner{
require(msg.sender == owner);
_;
}
function changeIt(uint _a)public onlyOwner{
a = _a;
}
}
发现在运行构造函数处提示VM Exception while processing transaction: invalid opcode错误。
改正方法如下:
pragma solidity ^0.4.24;//改成了24版本,同时也要改编译器的版本
contract modifierTest{
uint a=0;
address owner;
constructor() public{
owner = msg.sender;
}
modifier onlyOwner{
require(msg.sender == owner);
_;
}
function changeIt(uint _a)public onlyOwner{
a = _a;
}
}
然后将
修改后编译成功,运行不再报错。在改回原来的节点环境也能运行了。
博客内容讲述了在使用Solidity进行智能合约开发时遇到的VMException错误,具体表现为在构造函数处报错。问题根源在于使用的Solidity版本过旧。通过将Solidity版本升级到0.4.24并相应更新编译器,成功解决了此问题。修复后的代码能够正常编译且在不同节点环境中运行无误。
1038

被折叠的 条评论
为什么被折叠?



