-
Hello, There is a function used in the course in the SimpleStorage section that looks like this:
When you make a transaction to, lets say call addPerson(Tom, 1) and then call People([0]), you effectively get the results 1 and Tom. people.push(People(_favoriteNumber,_name)) works as intended. When I tried to include the that line to add a People object from inside of the contract manually like this:
I get the following error in my solidity code:
Is the syntax I'm trying to implement just wrong and I need to do it differently or am I missing something? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I did it on the Remix as below, it's no problem. You may show all your code . // SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
contract MyPeople {
struct People {
uint256 favNum;
string name;
}
People[] public people;
function addPerson (string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber,_name ));
people.push(People(10 , "Tom"));
}
} |
Beta Was this translation helpful? Give feedback.
-
For me, the error was a bit different. I had to put a semicolon after variable declaration for it to work. I am not sure why? // SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
contract SimpleStorage {
uint256 public favoriteNumber; // Initialised to zero
People public person = People({
favoriteNumber: 10,
name: "Patrick"
}); // This semi colon.
struct People {
uint256 favoriteNumber;
string name;
}
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
// view, pure function are not charged ay gas.
function retrieve() public view returns(uint256) {
return favoriteNumber;
}
}
|
Beta Was this translation helpful? Give feedback.
I did it on the Remix as below, it's no problem. You may show all your code .