Array returns "Undeclared identifier. Did you mean "Candidate" or "candidate"?" - solidity

when trying to set up an array of structs i get an error that says "Undeclared identifier. Did you mean "Candidate" or "candidate"?"
Ive tried to create an empty array by writing
uint[] memory candidate;
and
bytes[] memory candidate
above the function in which i'm getting errors to instantiate it. However, this doesn't work either and gives errors. It does not compile.
pragma solidity 0.5.0;
contract Election {
string public candidate;
struct Candidate {
uint id;
string name;
uint voteCount;
}
mapping(uint => Candidate) candidate;
uint public candidatesCount;
function createCandidate(string storage name ) private {
candidatesCount ++;
candidates[candidatesCount] = Candidate(candidatesCount, name, 0);
}
function addCandidates () public {
createCandidate("Candidate1");
createCandidate("Candidate2");
}
}

change
mapping(uint => Candidate) candidate;
to
mapping(uint => Candidate) candidates;
and
function createCandidate(string storage name ) private
to
function createCandidate(string memory name ) private

Related

solidity array mapping with array inside struct

Unable to execture createSchema function. It gives following error
revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Testing {
struct Schema {
mapping(string => string) entity;
}
struct SchemaMapping {
// mapping(string => string) key;
// mapping(string => string) value;
string[] key;
string[] value;
}
mapping(uint256 => Schema) schemas;
mapping(uint256 => SchemaMapping[]) schemaMappings;
function createSchema(uint256 id, string memory key, string memory value) public {
SchemaMapping[] storage schemamapping = schemaMappings[id];
schemamapping[id].key.push(key);
schemamapping[id].value.push(value);
schemas[id].entity[key] = value;
}
function getSchemaElemet(uint256 id) public view returns (SchemaMapping[] memory) {
return schemaMappings[id];
}
}
I adjusted your smart contract. The issue in your original contract is that you were trying to add values into schemaMapping without create SchemaMapping at specific index.
Smart contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "#openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Testing {
struct Schema {
mapping(string => string) entity;
}
struct SchemaMapping {
string[] key;
string[] value;
}
mapping(uint256 => Schema) schemas;
mapping(uint256 => SchemaMapping[]) schemaMappings;
function createSchema(uint256 id, string memory key, string memory value) public {
SchemaMapping[] storage schemamapping = schemaMappings[id];
// NOTE: I created an empty space in storage for create object and after bind it with values.
SchemaMapping storage singleSchemaItem = schemamapping.push();
// NOTE: I put values inside keys
singleSchemaItem.key.push(key);
singleSchemaItem.value.push(value);
schemas[id].entity[key] = value;
}
function getSchemaElemet(uint256 id) public view returns (SchemaMapping[] memory) {
return schemaMappings[id];
}
}

I keep on getting ParseError: Expected type name // when I want to return a struct I have just created

I am new in Solidity and I have been trying to create and get STRUCT without adding it to an Array. I alway see Structs with arrays and the method .push and I wanted to try it without it.
I have created a single Contract with one Struct and one function to create and get it.
If I create a single public function to create, but not return, the struct it doesn´t give me any error... Like the following:
struct Todo {
string name;
uint age;
}
function createTodo(string memory _name, uint _age) public pure{
Todo memory myTodo = Todo(_name, _age);
}
With the above code I would also like to know why it wouldn´t let me set the pointer "Todo" as storage like: Todo storage myTodo = Todo(_name, _age); It gives an TypeError: Todo memory is not implicity convertible to expect type struct storage pointer.
Next, I tried to modify the function to create and RETURN but then is when I start getting the ParseError.
The code is the following:
function getTodo(string memory _name, uint _age) public returns(struct myTodo) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
With the aobve code I also tried in "returns(struct), returns(struct memory)"....
I would really appreciate any type of help here.
Thank you very much
You received this error because you're wrong to set a returns keyword on getTodo() function. In your case, you must change your function in this way:
function getTodo(string memory _name, uint _age) external pure returns(Todo memory) {
Todo memory myTodo = Todo(_name, _age);
return myTodo;
}
If you want to handle storage structs, see this smart contract code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Test {
struct Todo {
string name;
uint age;
}
// Declare state variable
Todo[] todoArray;
// Push into array new struct
function createTodo(string memory _name, uint _age) public {
todoArray.push(Todo(_name, _age));
}
// Retrieve ToDo struct from specific index about struct array
function getTodo(uint _index) external view returns(Todo memory) {
return todoArray[_index];
}
}

TypeError: Member "push" is not available in struct SmartRegistryData.CDMD memory[] memory outside of storage

**// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract SmartRegistryData{
struct TP {
uint id;
}
struct OP {
uint id;
}
struct BP{
uint id;
}
//ContractDescriptionMetaData
struct CDMD {
uint ID;
address providerAdress;
TP TechnicalPerspective;
OP OperationalPerspective;
BP BusnissPerspective;
}
mapping(uint => CDMD) public CDMDS;
mapping(address => CDMD) public CDMDSA;
struct PROVIDER{
uint8 id;
string name;
address publicAddress;
string entrepriseName;
string entreprisedomain;
string entreprisedescription;
}
// Store provider
mapping( address => PROVIDER) public _listproviders;
function retreiveCDMDbyProvider(address _publicAddress, address publicAddress)public{
CDMD [] memory CDMDarray = new CDMD[](100);
// CDMD balance[] = new CDMD[](100);
CDMD storage cdmd = CDMDSA[_publicAddress];
if(CDMDSA[_publicAddress].providerAdress== publicAddress){
CDMD memory cdma = CDMDSA[_publicAddress];
//balance.push(cdma);
}
}
}**
when I'm typing --- truffle compile I get this error
how Can I push a struct into a dynamic array ??
there is another solution to retrieve a set of struct according to a specified attribute, such as address.

How can i push element on array defined on struct in Solidity?

I'm starting to approach with Solidity (current version 0.8.0) smart contracts, but I can't understand why the compiler print me an error during the push.
The problem is the following: I'm trying to push an element on array in struct but compile failed with error: Copying of type struct memory[] memory to storage not yet supported.
Here my example:
contract TestNFT is ERC721, Ownable {
struct Child {
string name;
}
struct Father {
string name;
Child[] childs;
}
mapping(uint256 => Child) private _childs;
uint256 nextChild = 0;
mapping(uint256 => Father) private _fathers;
uint256 nextFather = 0;
constructor(string memory name, string memory symbol)
ERC721(name, symbol)
{}
function mint(
string memory name
) public onlyOwner {
_safeMint(msg.sender, nextCharacter);
_childs[nextChild] = Child(name);
nextChild++;
}
function mintFather(string memory name) public onlyOwner {
_safeMint(msg.sender, nextClan);
_fathers[nextFather] = Father(name, new Child[](0));
nextFather++;
}
function insertChildToFather(uint256 fatherId, uint256 childId)
public
onlyOwner
{
Child memory child = _childs[childId];
_fathers[fatherId].childs.push(child);
}
}
How can I fix the insertChildToFather function?

Solidity Mapping use string to find struct?

I want to use the mapping find the struct but the mapping just can use the bytes and I need to use the string.
pragma solidity >=0.4.22 <0.6.0;
pragma experimental ABIEncoderV2;
contract land{
address public owner;
constructor() public{
owner = msg.sender;
}
struct Landpaper{
string number;
string landaddress;
string landnumber;
string landpurpose;
uint landgrades;
uint256 landarea;
string holdpoints;
}
mapping(bytes8 => Landpaper) public lp;
modifier Permission(){
require(msg.sender == owner);
_;
}
function set(string memory rfidnumber, Landpaper memory _landpaperRecord) public Permission{
lp[rfidnumber]=_landpaperRecord;
}
function get(string memory rfidnumber) view public returns(Landpaper memory){
return lp[rfidnumber];
}
}
because I have to read the rfid UID and I will translate the UID to string, so I need to use the string set my data. I use the bytes8 and I input string type, I need to change my string to bytes8, plz told me how to do.
You can write a function to convert string to bytes8
function stringToBytes8(string memory sourceStr) private pure returns(bytes8) {
bytes8 temp = 0x0;
assembly {
temp := mload(add(sourceStr, 32))
}
return temp;
}
And call it from set and get functions
function set(string memory rfidnumber, Landpaper memory _landpaperRecord) public Permission {
bytes8 rfidb8 = stringToByte8(rfidnumber);
lp[rfidb8]=_landpaperRecord;
}
function get(string memory rfidnumber) public view returns(Landpaper memory) {
bytes8 rfidb8 = stringToByte8(rfidnumber);
return lp[rfidb8];
}
Though I would suggest to avoid performing conversion process in contract as it would consume unnecessary gas. Instead perform the conversion on client side and pass bytes8 as parameter
function set(bytes8 rfidnumber, Landpaper memory _landpaperRecord) public Permission {
lp[rfidnumber]=_landpaperRecord;
}
function get(bytes8 rfidnumber) public view returns(Landpaper memory) {
return lp[rfidnumber];
}