我正在学习区块链和智能合约课程,我试图运行的代码似乎没有编译,尽管它与课程中的代码完全相同。我不能对这个程序说太多,因为我是一个新手,我只是想跟上,我不知道是语法错误还是其他原因。我只能说,我使用的是一个导入的智能合约,我在上一课中对其进行了编码,它运行得非常好。
特定错误: contracts/StorageFactory.sol:20:5:ParserError:应为pragma、import指令或contract/interface/library/struct/enum定义。函数sfGet(uint256_simpleStorageIndex(公共视图返回(uint256({^------^
代码1:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract SimpleStorage {
//this will get initialized to 0!
uint256 favoriteNumber;
bool favoriteBool;
struct People {
uint256 favoriteNumber;
string name;
}
People[] public people;
mapping(string => uint256) public nametoFavoriteNumber;
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
function retrieve() public view returns(uint256) {
return favoriteNumber;
}
function addPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber, _name));
nametoFavoriteNumber[_name] = _favoriteNumber;
}
}
代码2:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory {
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Address
// ABI
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorage.store(_simpleStorageNumber);
}
}
function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) {
SimpleStorage simpleStorage = SimpleStorage(address(SimpleStorageArray[_simpleStorageIndex]));
return simpleStorage.retrieve();
}
非常感谢。
在sfStore()
函数中,您在末尾放了两个花括号。这是您的代码:
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Address
// ABI
SimpleStorage simpleStorage = SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorage.store(_simpleStorageNumber);
}
} // <- THE ERROR IS HERE
删除一个花括号,就可以解决这个问题。新的sfStore
功能代码必须类似于:
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Address
// ABI
SimpleStorage simpleStorage =
SimpleStorage(address(simpleStorageArray[_simpleStorageIndex]));
simpleStorage.store(_simpleStorageNumber);
}