构造函数中 balances[] 的可靠性错误,"Index expression cannot be omitted"
Solidity error with balances[] in constructor, "Index expression cannot be omitted"
Solidity 和智能合约的新手,一直在尝试在 Windows 上设置测试环境,但在尝试测试 运行 松露盒装环境 (MetaCoin) 时遇到了困难。
在下面的构造函数中,balances[] 做错了。我的 IDE(具有 solidity 扩展的 vscode)抛出一条消息:"Index expression cannot be omitted"。为什么会出现这个?
contract MetaCoin {
mapping (address => uint) public balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
constructor() public {
balances[] = 10000;
}
function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function getBalanceInEth(address addr) public view returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}
function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
此外,如果我能以某种方式解决这个问题,下一步是什么?我如何在账户之间进行转账才能在工作中看到它?
我一直在关注这个指南:https://medium.com/edgefund/ethereum-development-on-windows-part-1-da260f6a6c06
mapping (address => uint) public balances;
balances 是从地址到 uint 的映射,但在您的构造函数中您没有提供任何地址来将 uint 设置为。
您可以添加 msg.sender
,这样它会将初始余额设置为部署合约的地址。
balances[msg.sender] = 10000;
对于第二个问题你可以直接调用sendCoin
函数
Solidity 和智能合约的新手,一直在尝试在 Windows 上设置测试环境,但在尝试测试 运行 松露盒装环境 (MetaCoin) 时遇到了困难。
在下面的构造函数中,balances[] 做错了。我的 IDE(具有 solidity 扩展的 vscode)抛出一条消息:"Index expression cannot be omitted"。为什么会出现这个?
contract MetaCoin {
mapping (address => uint) public balances;
event Transfer(address indexed _from, address indexed _to, uint256 _value);
constructor() public {
balances[] = 10000;
}
function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}
function getBalanceInEth(address addr) public view returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}
function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
此外,如果我能以某种方式解决这个问题,下一步是什么?我如何在账户之间进行转账才能在工作中看到它?
我一直在关注这个指南:https://medium.com/edgefund/ethereum-development-on-windows-part-1-da260f6a6c06
mapping (address => uint) public balances;
balances 是从地址到 uint 的映射,但在您的构造函数中您没有提供任何地址来将 uint 设置为。
您可以添加 msg.sender
,这样它会将初始余额设置为部署合约的地址。
balances[msg.sender] = 10000;
对于第二个问题你可以直接调用sendCoin
函数